Dirty nixos-rebuild build --flake issues

You can try explicitly specifying the flake to be of type path. The default is git, which uses a git repository as its input that can be a local path, but can also be a URL.

nixos-rebuild build --flake path:.#xps15 --impure

Potentially, a relative path flake doesn’t work and you have to make the path absolute:

nixos-rebuild build --flake path:$PWD#xps15 --impure

The reason for this is that Nix Flakes do everything they can to be reproducible, so when you build a git flake, it uses the current git HEAD as the source (so the last commit) AFAIK (corrected by @noah, it’s actually the filesystem tree, but only tracked files), not the current content of the directory. This means that just with the git hash (which is what git flakes use for locking), the flake is fully reproducible.

With a path flake, it uses the NAR hash for locking, which ensures that a change in the flake directory is detected, but this does not allow reproducing the result unless you also have the full source in your store.

Another workaround you can use for this is with work-in-progress commits and the --amend flag:

# Before you start a "project"
git commit --allow-empty -m "WIP" # Create empty WIP commit

# After every change
nix fmt && 
git add . &&
git commit --amend --no-edit &&
nixos-rebuild build --flake .#xps15 --impure

# If it built successfully
sudo nixos-rebuild switch --flake .#xps15 --impure 

# Once you're happy
git commit --amend # Edit commit message
git push

I use aliases for amends because they’re really useful in general.

2 Likes