• Hints & notes related to NixOS, Nix & friends

  • Random useful commands

  • Random Nix language snippets

  • 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 installable my-derivation.nix

    TODO: I recall it’s possible to do an “universal header”, working both for installable or importable derivation, but what was the incantation?…

    with import <nixpkgs> {};
    
    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":12674436,"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 - [NixOS manual](https://nixos.org/nixos/manual/)\n"},{"_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":"76785bf4c482a17d7100008c","treeId":"76788bf0c482a17d7100006c","seq":12665494,"position":1,"parentId":"76786209c482a17d7100008a","content":"### Header for installable *my-derivation.nix*\n\n*TODO: I recall it's possible to do an \"universal header\", working both for installable or importable derivation, but what was the incantation?...*\n\n with import <nixpkgs> {};\n\n stdenv.mkDerivation {\n ...\n }\n\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"}}