Skip to content

Instantly share code, notes, and snippets.

@monadplus
Last active November 13, 2019 08:16
Show Gist options
  • Save monadplus/34870c4f487e4f444a9fb0c095d87f45 to your computer and use it in GitHub Desktop.
Save monadplus/34870c4f487e4f444a9fb0c095d87f45 to your computer and use it in GitHub Desktop.
Nix: useful commands

show-derivation:

$ nix show-derivation /nix/store/kg2g9q5chm6dyn2hh8pk8zxwkgb4ph7q-myname.drv

{
  "/nix/store/kg2g9q5chm6dyn2hh8pk8zxwkgb4ph7q-myname.drv": {
    "outputs": {
      "out": {
        "path": "/nix/store/bgzq8iyk93c2cxpcsqapfrwij5kwfnpc-myname"
      }
    },
    "inputSrcs": [],
    "inputDrvs": {
      ....
}

When is the derivation built

Nix does not build derivations during evaluation of Nix expressions. In fact, that's why we have to do :b drv in nix repl, or use nix-store -r in the first place.

An important separation is made in Nix:

  • Instantiate/Evaluation time: the Nix expression is parsed, interpreted and finally returns a derivation set. During evaluation, you can refer to other derivations because Nix will create .drv files and we will know out paths beforehand. This is achieved with nix-instantiate.

  • Realise/Build time: the .drv from the derivation set is built, first building .drv inputs (build dependencies). This is achieved with nix-store -r.

Think of it as of compile time and link time like with C/C++ projects. You first compile all source files to object files. Then link object files in a single executable.

In Nix, first the Nix expression (usually in a .nix file) is compiled to .drv, then each .drv is built and the product is installed in the relative out paths.

Monorepo tutorial

$ nix-shell -p '(import <nixpkgs> {}).haskellPackages.ghcWithPackages (pkgs: [ pkgs.lenses pkgs.aeson ])'
shell> ghc-pkg list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment