When is a path in a Nix expression copied into the Nix store?

Is there a list?

For example:

  • “Nix path to string” scenarios1:

    • builtins.toString ./a_file
      returns absolute path to a_file as a string (or an error if it doesn’t exist
      not copied to the Nix store

    • string interpolation (e.g., "${./a_file}")
      coptied to the Nix store and returns the absolute path to it as a string

  • “bare” Nix paths in mkShell when assigning to environment variables:

    { pkgs ? import <nixpkgs> {} }:
    
    pkgs.mkShell {
      buildInputs = [
        # ...
      ];
    
      # environment variable
      GIT_CONFIG_GLOBAL = ./assets/git.conf;
    
      # equivalent to:
      shellHook =
        let
          gitConf =
            pkgs.writeText
              "git.conf"
              (builtins.readFile ./assets/git.conf)
          ;
        in
          ''
          #                         VV.......V
          export GIT_CONFIG_GLOBAL="${gitConf}"
          #                         ^^.......^
          #
          # That's Nix's string interpolation!
          # Use ''\{..} for Bash's string interpolation.
          ''
      ;
    }
    
  • Haven’t packaged anything yet with Nix, but what I remember from the manuals is that any path that appears in builder shell scripts will get put into the Nix store as well. (Right?)


[1]: Relevant threads:

1 Like

You’re in luck, thanks to funding by Antithesis we expanded on that subject the manual in the past months:

https://nixos.org/manual/nix/stable/language/operators.html#string-concatenation

https://nixos.org/manual/nix/stable/language/string-interpolation.html

https://nixos.org/manual/nix/stable/language/values.html#type-path

https://nixos.org/manual/nix/stable/language/builtins.html#builtins-toString

Right. Paths are baked into derivations to make them immutable.

1 Like

Thank you for collecting these Nix manual pages! It’s been a while since I read through the manual, but they have changed quite a lot, and now have significantly more practical info in them. Thank you for all of you who contributed!

1 Like