Tue Oct 29 15:29:38 EDT 2019

revisit basics -- nix FAQ

Time to revisit.  Here is a list of things I did not understand
properly before writing this down.

1. It is very confusing that nix-shell / nix-build takes one type of
   default.nix, and nixpkgs' default.nix is something else.

   One takeaway: default.nix is the nix expression that is associated
   to a directory.  That's pretty much it.  The type of this can be

   For now, focus on what goes into nix-shell, then generalize.

2. nix-shell interprets the nix file as a function that takes
   arguments to an expression of type Derivation.  ( I'm using captial
   letters to denote types, which are not part of nix syntax but just
   a way to think about expressions ).

3. what does nix-shell pass in as arguments?  is there a way to print
   the args?  TODO: explain.

4. what i see most often is something like this: the derivation is
   produced by a default nixpkgs.stdenv, but it is possible to
   override that.

   { pkgs ? import ../deps/nixpkgs {} }:
   pkgs.stdenv.mkDerivation { name = "doodle1"; }

5. the link between nix expressions and /nix/store/*.drv is the
   built-in function "derviation".  the .drv file is a simpler format
   that instructs nix how to build something.

6. nix-shell doesn't actually build the thing described by the
   derivation.  it stops just short of building: it drops you in an
   environment that has all the build dependencies specified in
   "buildInputs" and "nativeBuildInputs".  (see
   https://nixos.org/nixpkgs/manual/ for an explanation of those) rule
   of thumbs: latter is compilers, former is anything else,
   e.g. run-time libs.

7. how to understand this syntax?

   targetPkgs = pkgs: with pkgs; [ bash bc ];

   it's equivalent to

   targetPkgs = [ pkgs.bash pkgs.bc ];

   Two components:

   - 'with' introduces a dictionary elements as variables in the
     lexical scope of an expression, so the list expression can
     reference those names directly

   - the 'pgks:' part seems to indicate that the type of targetPkgs is
     a function that takes a dictionary with packages as an argument.

   That seems to correspond to what I see in:

8. the attributes that are passed into stdenv.mkDerivation are mapped
   to shell variables.  e.g. "echo $buildInputs"

9. to install a nix file's product into the current environment

   nix-env -i -f default.nix