Use nix file excluded from git?

i have a system flake project checked in git.
if i have files not staged in git, it will complain it can’t find them until i do.
now my question is: can i circumvent that restriction?

the use-case for this i thought up is, having a nix file containing enable toggles excluded from git. as such, potentially git bisect operations could be sped up a little by temporarily disabling some features the user deems not relevant to the bisect.

it feels like i’m trying stuff you’re not supposed to want so far :see_no_evil:, and i kinda had trouble finding documentation on potential ways to get around this.

1 Like

You can have a second flake as input that is not a git repo, and have that repo provide a module which flips those switches.

Awkward whenever you need to change them, though.

Alternatively, you can explicitly use the path: prefix to your flake URL to make your git repo be treated as if it was not a git repo.


I’m not entirely sure what you’re looking for, but maybe you could benefit from some source filtering (using lib.fileset): Working with local files — documentation

1 Like

Furthermore, with stable Nix and lib.fileset, it’s possible to import only Git-tracked files into the store, but also add extra files using

lib.fileset.unions [
  # Include all git-tracked files
  (lib.fileset.gitTracked ./.)
  # Plus another one

@Infinisil i feel i’m sorta missing context on usage here. where might one perhaps invoke this in a basic system flake?
i see your blog post mention mkDerivation, so i imagine that’s about the level this would function at, but iiuc that appears somewhat abstracted away by the nixpkgs.lib.nixosSystem commonly used to do system flakes.

Ah yeah you didn’t mention the context, so I wasn’t sure if it would help. lib.fileset is good for filtering local sources of derivations. So indeed, because that doesn’t exist for a NixOS system, it’s not generally useful there.

1 Like

There’s an open PR to nix to allow for disabling this restriction: Add option to include untracked files in git and mercurial fetchers by jfroche · Pull Request #9352 · NixOS/nix · GitHub

You could build nix yourself with this patch included.
Be aware though, you might end up accidentally importing big files into your store (like if you have a .qcow2 image from a VM run lingering around in your source dir).

hm, looks like the mentioned open PR does not pertain to files in .gitignore, unfortunately. :frowning:
it looks like @TLATER’s advice seems to help. i’ll try and figure out how to use that using relative file paths as well.

i achieved this now by reading environment variable PWD, requiring impure evaluation.

You could just use path-type…

this default approach unfortunately fails for files not staged in git, so it looks like you’d need to either use workarounds or trick nix into not considering it a git repo somehow

Yes, the path type.

nix build path:. should do…

1 Like