Nix

Table of contents:

nix-shell

Call an external package as python module

Just use paranthesis with callpackage:

nix-shell -p 'python3.withPackages(ps : with ps; [ pygobject3 (callPackage ~/myextramodule.nix {}) ipython ])'

Overlay/override example

Let's change "version" attr of glibc and override Fabric python module for a different version

let
  overlay = (self: super: rec {
    glibc = super.glibc.overrideAttrs (_: {
      # Warning: MASSIVE rebuild since you'll break ABI
      version = "2.26";
    });
    python3 = super.python3.override {
      packageOverrides = self: super: {
        Fabric = super.Fabric.overrideAttrs (old: rec{
          version = "1.14.post1";
          doInstallCheck = false;
          src =  super.fetchPypi {
            pname = "Fabric3";
            inherit version;
            sha256 = "108ywmx2xr0jypbx26cqszrighpzd96kg4ighs3vac1zr1g4hzk4";
          };
        });
      };
    };

    python3Packages = python3.pkgs;
  });

  # Another one, adding a patch
  krunner-pass = pkgs.krunner-pass.overrideAttrs (attrs: {
    patches = attrs.patches ++ [ ~/some/placepass-dbus.patch ];
  });

  # Let's put together a package set to use later
  myPythonPackages = ps: with ps; [
    Fabric
    # and other modules you'd like to add
  ];
in
{ pkgs ? import <nixpkgs> { overlays = [ overlay ]; } }:
pkgs.mkShell {
  buildInputs = with pkgs; [
    (python3.withPackages myPythonPackages)
    krunner-pass # patched version above will be used
    # and other system packages you'd like to add
  ];
}

Run postgresql database

{ pkgs ? import <nixpkgs> {}, ... }:
let
  inherit (pkgs) stdenv;
in stdenv.mkDerivation {
  name = "myproject-devenv";
  buildInputs = with pkgs; [
    postgresql_11
  ];
  shellHook = ''
    export PGDATA=$PWD/postgres_data
    export PGHOST=$PWD/postgres
    export LOG_PATH=$PWD/postgres/LOG
    export PGDATABASE=postgres
    export DATABASE_URL="postgresql:///postgres?host=$PGHOST"
    if [ ! -d $PGHOST ]; then
      mkdir -p $PGHOST
    fi
    if [ ! -d $PGDATA ]; then
      echo 'Initializing postgresql database...'
      initdb $PGDATA --auth=trust >/dev/null
    fi
    pg_ctl start -l $LOG_PATH -o "-c listen_addresses= -c unix_socket_directories=$PGHOST"
  '';
}

Use a package from different overlay

let
  unstable = import (fetchTarball https://nixos.org/channels/nixos-unstable/nixexprs.tar.xz) { };
in
{ nixpkgs ? import <nixpkgs> {} }:
with nixpkgs; mkShell {
  buildInputs = [ unstable.python37Packages.python-twitter ];
}

Misc

Use local nixpkgs clone

export NIX_PATH=nixpkgs=~/devel/nixpkgs

Run any app from any channel

nix run --ignore-environment -k DISPLAY -k XDG_RUNTIME_DIR -k XAUTHORITY -k LD_LIBRARY_PATH -f channel:nixpkgs-unstable zoom-us -c zoom-us

See which packages are causing big disk usage

sudo nix-du -s=500MB | tred | dot -Tpng > store.png