Nix

<< Back to wiki homepage

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 ])'

Override example (optional args)

nix-shell -p 'inxi.override { withRecommends = true; }'

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

Above command seems to be deprecated in favor of nix-shell, so here is the new stuff

nix-shell -p slack -I nixpkgs=channel:nixpkgs-unstable --run "slack -s"

See which packages are causing big disk usage

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

Repair corrupted store/db

sudo nix-store --verify --check-contents --repair

Import your configuration into nix repl

nix repl '<nixpkgs/nixos>' -I nixos-config=./${your entry point}.nix

Import a specific flake directory to nix repl

nix repl --expr "builtins.getFlake \"${DIRECTORY_OF_FLAKE}\""

Find store path of any package

nix eval nixos.${pkgname}.outPath