Skip to content

Impurity: Hash of relative paths depends on parent directory name #166

Closed

Description

@rickynils

Consider the following Nix expression, stored in default.nix:

with (import <nixpkgs> {});

writeText "out" ''
  ${./.}
''

Then look at the following console session:

/tmp/nixtest $ nix-build --no-out-link
these derivations will be built:
  /nix/store/wvarg2d7f0dhfgz0wvwhm8ibzkb1pfrc-out.drv
building path(s) `/nix/store/1664687s61dyki8b1pqw8fa1k2314pwz-out'
�building /nix/store/1664687s61dyki8b1pqw8fa1k2314pwz-out
�/nix/store/1664687s61dyki8b1pqw8fa1k2314pwz-out
/tmp/nixtest $ cd ..
/tmp $ mv nixtest/ nixtest2
/tmp $ cd nixtest2/
/tmp/nixtest2 $ nix-build --no-out-link
these derivations will be built:
  /nix/store/piwxsvbn6myy4xajazg0pwlh7k46sfzq-out.drv
building path(s) `/nix/store/4z5vzja4gk0mifrn4dxd2d17fwfsy86w-out'
�building /nix/store/4z5vzja4gk0mifrn4dxd2d17fwfsy86w-out
�/nix/store/4z5vzja4gk0mifrn4dxd2d17fwfsy86w-out
/tmp/nixtest2 $ cd ..
/tmp $ mv nixtest2/ nixtest
/tmp $ cd nixtest/
/tmp/nixtest $ nix-build --no-out-link
/nix/store/1664687s61dyki8b1pqw8fa1k2314pwz-out

As you can see, the hash of the build depends on the name of directory that contains default.nix. I had expected that only the files and directories contained inside this directory would matter. This is an impurity, I would say, since you can't recreate a build if you don't know the name of the directory that the build was originally performed in.

Activity

  1. edolstra commented on Sep 4, 2013

    @edolstra
    Member

    That's not really an impurity. As long as the name and contents of the file are the same, you should get the same hash. That the name might change is simply a reason not to use ./. in this case.

  2. rickynils commented on Sep 4, 2013

    @rickynils
    MemberAuthor

    But the parent directory is not really part of the content, I think. If I clone Nix sources from github, should it really impact my build what I named the target directory?

    My example may seem contrived, but I discovered this when building NixOS. I keep different NixOS configurations in the following directory layout:

    /.../machine1/default.nix
    /.../machine1/configuration.nix
    /.../machine1/nixos
    /.../machine1/nixpkgs
    /.../machine2/default.nix
    /.../machine2/configuration.nix
    /.../machine2/nixos
    /.../machine2/nixpkgs
    

    My default.nix files contains the following:

    let
      nixos = import ./nixos {
        nixpkgs = ./nixpkgs;
        configuration = ./configuration.nix;
      };
    in {
      inherit (nixos) system;
    }
    

    Then I can build a system in the following way:

    cd machine1
    nix-build -A system
    

    But if I change the names of the machine1 or machine2 directories, I get a rebuild even though nothing that should have an impact on the build has changed.

  3. edolstra commented on Feb 26, 2014

    @edolstra
    Member

    Closing since this is intended (if not always desirable) behaviour.

  4. added a commit that references this issue on Jan 1, 2024
  5. added a commit that references this issue on Aug 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions