Sign up for free to use this document yourself.
  • Hints & notes related to NixOS, Nix & friends

  • Flakes & nix 2.0 stuff

  • Random useful commands

  • Random Nix language snippets

  • $ nix build -L --keep-failed .#example
    • -L — show all logs
    • --keep-failed — in case of failure, keep build directory in:
      /tmp/nix-build-$ATTRNAME.drv-$NUM
    • .#example (optional) — . is directory with flake.nix; example picks the attribute from it if not default.
      Both are optional, . is default, and “defaultPackage” is picked if no attr is specified.
  • $ nix search nixpkgs firefox
  • Ad-hoc package management — nix-env

    List available (installable) packages matching a pattern, with status (via):

    $ nix-env -qas '.*firefox.*'

    Note:

    • I in result means “installed”, others are irrelevant.

    List installed packages:

    $ nix-env -q
    $ nix-env -qs  # with status

    Install a package:

    $ nix-env -i firefox
    $ nix-env -iA nixpkgs.firefox   # faster

    or from file:

    $ nix-env -if my-derivation.nix

    Uninstall (evict) a package:

    $ nix-env -e firefox
  • Environments or packages debugging — nix-shell

    Debug a failing nix-env package with nix-env:

    $ nix-env ... -K
    ...
    note: keeping build directory '/tmp/nix-build-...drv-0'
    builder for ... failed with exit code 1
    error: build of '...' failed
    $ ls /tmp/nix-build-*.drv-0/

    Debug a failing nix-env package with nix-shell:

    $ nix-shell my-package.nix
    [nix-shell]$ type genericBuild  # informational
    genericBuild is a function
    ...
  • “Universal header” for derivations (WIP)

    { pkgs ? (import <nixpkgs> {})
    , stdenv ? pkgs.stdenv
    , fetchFromGitHub ? pkgs.fetchFromGitHub
    }:
    
    stdenv.mkDerivation { ... }
  • Header for nix-shell-based “environments”

    Example:

    #!/usr/bin/env nix-shell
    #! nix-shell shell-sile-math.nix --show-trace
    
    with import <nixpkgs> {};
    
    stdenv.mkDerivation {
      name = "foobar-env";
      buildInputs = [
        lua5_2
        luarocks
        lua52Packages.luastdlib
      ];
      inherit (lua52Packages) luastdlib;
      LUA_PATH = "${luastdlib}/share/lua/${lua.luaversion}/?.lua;${luastdlib}/share/lua/${lua.luaversion}/?/init.lua";
      ...
    }
{"cards":[{"_id":"76788a47c482a17d71000082","treeId":"76788bf0c482a17d7100006c","seq":12665326,"position":1,"parentId":null,"content":"# Hints & notes related to [NixOS](https://nixos.org), [Nix](https://nixos.org/nix) & friends\n"},{"_id":"767887d3c482a17d71000085","treeId":"76788bf0c482a17d7100006c","seq":12665329,"position":2,"parentId":null,"content":"## Important links"},{"_id":"767886abc482a17d71000086","treeId":"76788bf0c482a17d7100006c","seq":22031748,"position":1,"parentId":"767887d3c482a17d71000085","content":" - [Nix manual](https://nixos.org/nix/manual/) — especially:\n - [IV. Writing Nix expressions](https://nixos.org/nix/manual/#chap-writing-nix-expressions) contains tutorial for `std.mkDerivation`. More details follow in Nixpkgs manual, see below.\n - [IV.15. Nix Expression Language](https://nixos.org/nix/manual/#ch-expression-language) describes syntax of Nix expressions.\n - [IV.15.5. Built-in Functions](https://nixos.org/nix/manual/#ssec-builtins) lists basic functions available in Nix language (the basic building blocks).\n - [III.11. Channels](https://nixos.org/nix/manual/#sec-channels) is a tutorial on `nix-channel`.\n - Nixpkgs manual\n - [3. The Standard Environment](https://nixos.org/nixpkgs/manual/#chap-stdenv) is a more detailed look at options available in `std.mkDerivation`, extending the tutorial from the Nix manual\n - [III. Builders](https://nixos.org/manual/nixpkgs/stable/#idm140737319395328), incl. [11. Fetchers](https://nixos.org/manual/nixpkgs/stable/#chap-pkgs-fetchers) like `fetchGitHub`, [12. Trivial builders](https://nixos.org/manual/nixpkgs/stable/#chap-trivial-builders) like `writeScriptBin`, and more (Python support, Steam support, etc.)\n - [NixOS manual](https://nixos.org/nixos/manual/)\n"},{"_id":"404aaaf11fc4049bc300001b","treeId":"76788bf0c482a17d7100006c","seq":22031689,"position":2,"parentId":"767887d3c482a17d71000085","content":"### Code links\n\n- [build phases](https://github.com/NixOS/nixpkgs/blob/046f8835dcb9082beb75bb471c28c832e1b067b6/pkgs/stdenv/generic/setup.sh#L1245) in `pkgs/stdenv/generic/setup.sh`"},{"_id":"40a662008ab94386a8000037","treeId":"76788bf0c482a17d7100006c","seq":22001678,"position":2.5,"parentId":null,"content":"## Flakes & nix 2.0 stuff\n"},{"_id":"40a65fb78ab94386a800003a","treeId":"76788bf0c482a17d7100006c","seq":22001763,"position":0.5,"parentId":"40a662008ab94386a8000037","content":" $ nix build -L --keep-failed .#example\n\n * `-L` &mdash; show all logs\n * `--keep-failed` &mdash; in case of failure, keep build directory in: \n `/tmp/nix-build-$ATTRNAME.drv-$NUM`\n * `.#example` (optional) &mdash; `.` is directory with flake.nix; `example` picks the attribute from it if not default. \n Both are optional, `.` is default, and \"defaultPackage\" is picked if no attr is specified."},{"_id":"40a661038ab94386a8000039","treeId":"76788bf0c482a17d7100006c","seq":22001683,"position":1,"parentId":"40a662008ab94386a8000037","content":" $ nix search nixpkgs firefox"},{"_id":"767874a2c482a17d71000088","treeId":"76788bf0c482a17d7100006c","seq":12665435,"position":3,"parentId":null,"content":"## Random useful commands"},{"_id":"76787439c482a17d71000089","treeId":"76788bf0c482a17d7100006c","seq":12665488,"position":1,"parentId":"767874a2c482a17d71000088","content":"## Ad-hoc package management — *nix-env*\n\n**List available** (installable) packages matching a pattern, with status ([via](https://nixos.org/nix/manual/#ch-basic-package-mgmt)):\n\n $ nix-env -qas '.*firefox.*'\n\nNote:\n - `I` in result means \"installed\", others are irrelevant.\n\n**List installed** packages:\n\n $ nix-env -q\n $ nix-env -qs # with status\n\n**Install** a package:\n\n $ nix-env -i firefox\n $ nix-env -iA nixpkgs.firefox # faster\n\nor from file:\n\n $ nix-env -if my-derivation.nix\n\n**Uninstall (evict)** a package:\n\n $ nix-env -e firefox"},{"_id":"766cbdb1c482a17d7100008f","treeId":"76788bf0c482a17d7100006c","seq":12674350,"position":2,"parentId":"767874a2c482a17d71000088","content":"### Environments or packages debugging — *nix-shell*\n\n**Debug** a failing nix-env package with *nix-env*:\n\n $ nix-env ... -K\n ...\n note: keeping build directory '/tmp/nix-build-...drv-0'\n builder for ... failed with exit code 1\n error: build of '...' failed\n $ ls /tmp/nix-build-*.drv-0/\n\n**Debug** a failing nix-env package with *nix-shell*:\n\n $ nix-shell my-package.nix\n [nix-shell]$ type genericBuild # informational\n genericBuild is a function\n ...\n"},{"_id":"76786209c482a17d7100008a","treeId":"76788bf0c482a17d7100006c","seq":12665483,"position":4,"parentId":null,"content":"## Random Nix language snippets"},{"_id":"6db6fa7e3d35d6f31600002d","treeId":"76788bf0c482a17d7100006c","seq":14548427,"position":0.5,"parentId":"76786209c482a17d7100008a","content":"### \"Universal header\" for derivations (WIP)\n\n```nix\n{ pkgs ? (import <nixpkgs> {})\n, stdenv ? pkgs.stdenv\n, fetchFromGitHub ? pkgs.fetchFromGitHub\n}:\n\nstdenv.mkDerivation { ... }\n```"},{"_id":"76785422c482a17d7100008d","treeId":"76788bf0c482a17d7100006c","seq":12674417,"position":2,"parentId":"76786209c482a17d7100008a","content":"### Header for *nix-shell*-based \"environments\"\n\nExample:\n\n #!/usr/bin/env nix-shell\n #! nix-shell shell-sile-math.nix --show-trace\n\n with import <nixpkgs> {};\n\n stdenv.mkDerivation {\n name = \"foobar-env\";\n buildInputs = [\n lua5_2\n luarocks\n lua52Packages.luastdlib\n ];\n inherit (lua52Packages) luastdlib;\n LUA_PATH = \"${luastdlib}/share/lua/${lua.luaversion}/?.lua;${luastdlib}/share/lua/${lua.luaversion}/?/init.lua\";\n ...\n }"}],"tree":{"_id":"76788bf0c482a17d7100006c","name":"nix","publicUrl":"nix"}}