Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to prevent flake inputs source from garbage collected #3995

Open
zanculmarktum opened this issue Sep 7, 2020 · 8 comments
Open

Option to prevent flake inputs source from garbage collected #3995

zanculmarktum opened this issue Sep 7, 2020 · 8 comments
Labels

Comments

@zanculmarktum
Copy link

Something similar to keep-outputs = true would be nice 😄

@edolstra
Copy link
Member

For this we would need to decide what the GC roots are. E.g. if you run nix run nixpkgs#hello, should the nixpkgs flake become a GC root? And when do we release that root?

BTW we had some flake GC root registration, but it had to be removed because it's not compatible with lazy flake input fetching (1b49479).

@stale
Copy link

stale bot commented Sep 14, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the stale label Sep 14, 2021
@ncfavier
Copy link
Member

ncfavier commented Sep 26, 2021

This is important to me: the use case is being able to run nixos-rebuild offline after GC.

Why is this incompatible with lazy fetching? Adding a GC root to a path shouldn't require fetching it

@SuperSandro2000
Copy link
Member

For this we would need to decide what the GC roots are. E.g. if you run nix run nixpkgs#hello, should the nixpkgs flake become a GC root? And when do we release that root?

Could we make gcroots depend on paths and an argument?
eg doing something like nixos-rebuild build --add-flake-gc-root then always the latest download is kept?

@ruuda
Copy link

ruuda commented Jan 9, 2023

A use case for this is nix develop. I have many projects with a flake.nix now, and a devShell in there with packages from Nixpkgs.

  • I enter the development environment with nix develop --profile devenv-root, which creates a GC root.
  • Then I run nix store gc.
  • Now I want to enter the development environment again without an internet connection ... but this is impossible! The GC threw away the flake inputs, I suppose that prevents Nix from computing the store path of the devShell.

For this we would need to decide what the GC roots are. E.g. if you run nix run nixpkgs#hello, should the nixpkgs flake become a GC root?

I think in general if you add a GC root for something that you build though nix build (whether that’s a devShell or an application), then you want to keep the inputs to the flake alive, because without them we can’t determine what the store path of the final artifact is, so even if that one is still alive, if you can’t reach it, it’s useless to you. (Unless you are are storing a reference to it through some other means.)

And when do we release that root?

Maybe flake operations should create an “aggregate root”? One that keeps both the output and the flake inputs alive? It could be as simple as another store path that references the flake inputs as well as the output. nix develop --profile might make the profile link point to such a store path, and then nix develop would not need to re-download Nixpkgs after a GC.

(Side note, it is possible to get the store paths of the inputs with nix flake archive, so we can create GC roots that way. I do it like this in my custom “enter development shell with GC root” function, but it feels like a gross hack.)

@Aleksanaa
Copy link
Member

Any workarounds on this?

@ncfavier
Copy link
Member

ncfavier commented May 6, 2023

  collectFlakeInputs = input:
    [ input ] ++ concatMap collectFlakeInputs (builtins.attrValues (input.inputs or {}));

Then, in a module that uses inputs.foo:

  system.extraDependencies = collectFlakeInputs inputs.foo;

@Tommimon
Copy link

To add to the previous answer, if you want to prevent all flake sources from being garbage collected, you can just paste this somewhere in your configuration:

# Don't garbage collect flakes sources
system.extraDependencies = let
    collectFlakeInputs = input: [ input ] ++ builtins.concatMap collectFlakeInputs (builtins.attrValues (input.inputs or {}));
in
    builtins.concatMap collectFlakeInputs (builtins.attrValues inputs);

oxalica added a commit to oxalica/nixos-config that referenced this issue May 23, 2024
Nothing is more upset than reconfiguring Internet on Internet
outage requires Internet access :(

Ref: NixOS/nix#3995 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants