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

Declarative env management with nix profile is too hard #11544

Open
dukc opened this issue Sep 19, 2024 · 11 comments
Open

Declarative env management with nix profile is too hard #11544

dukc opened this issue Sep 19, 2024 · 11 comments
Labels
new-cli Relating to the "nix" command profiles Versioned gc root symlinks; nix profile, nix-env

Comments

@dukc
Copy link

dukc commented Sep 19, 2024

Describe the bug

So, I'm wanting to migrate my personal Nix configurations to flakes. I figured out that since the new-gen nix commands are needed to understand flakes, I should migrate my commands before I start rewriting my .nix files.

I'm managing my user environment declaratively, pretty much how the NixPkgs manual suggests except I put the buildEnv derivation as the top-level expression of the file. By running nix-env -irf file.nix the user environment gets rebuilt transactionally in a single step, just like NixOS gets with sudo nixos-rebuild switch.

Now, turns out the experience of trying to do the same with nix profile is subpar. First off, I can't do it transactionally with one command. I have to write nix profile remove '.*' followed by nix profile install -f file.nix.

If I try writing a bash script to automate this there's another problem. If the installation of the new profile fails, I want to roll back. But how is the script supposed to know what profile number to roll back to? There is no (documented) command to query the number of the current profile before the remove command. nix profile history indicates the current generation only by colour of the profile number in question.

Maybe with enough bash-fu I could extract the coloured number but frankly, this is way too hard. Unless I'm mistaken, I should be able to use nix profile as a drop-in replacement for nix-env. This is pretty far from it.

nix-env --version output
nix-env (Nix) 2.18.5

@dukc dukc added the bug label Sep 19, 2024
@dukc

This comment was marked as resolved.

@roberth

This comment was marked as resolved.

@dukc

This comment was marked as resolved.

@roberth
Copy link
Member

roberth commented Sep 19, 2024

Can this be made to work with nix profile upgrade?

The documentation is rather flake-oriented, but does list -f/--file as an option.

@roberth roberth added new-cli Relating to the "nix" command profiles Versioned gc root symlinks; nix profile, nix-env labels Sep 19, 2024
@roberth roberth added this to the nix-command stabilisation milestone Sep 19, 2024
@dukc
Copy link
Author

dukc commented Sep 19, 2024

I did experiment with nix-env -u --leq before migrating, figuring that if that works nix profile upgrade would work too and vice-versa. it didn't work.

Probably it would work with some change to my overall scheme, but it's clearly not very easy way to go either as I have no idea what precisely I would have to change.

@dukc
Copy link
Author

dukc commented Sep 19, 2024

The documentation is rather flake-oriented, but does list -f/--file as an option.

I can confirm the -f option does work, and therefore nix profile is at least usable. But I can't upgrade my whole profile transactionally in one go like I can with nix-env.

@edolstra
Copy link
Member

If your profile consists of only one buildEnv derivation, you're probably better off using nix build --profile /path/to/profile --file file.nix.

@edolstra edolstra removed the bug label Sep 19, 2024
@dukc
Copy link
Author

dukc commented Sep 19, 2024

Huh, I can set my profile with nix-build/nix build? I'd certainly have tried if I knew! I'll try.

@dukc
Copy link
Author

dukc commented Sep 19, 2024

Doesn't seem to work. I removed one package from my file.nix and ran nix build --profile $NIX_USER_PROFILE_DIR -f file.nix.

It said no priviledge to do so. That's already bad, I shouldn't need super user priviledges to change my own user environment. Still, i attempted the same command with sudo, and it didn't do anything. Am I doing something wrong?

@dukc
Copy link
Author

dukc commented Sep 19, 2024

Never mind! the correct command was nix build --profile ~/.local/state/nix/profiles/profile -f file.nix, works and without sudo as it should. Thanks!

So my problem is solved. The question remains though whether it's easy enough for the user to figure out s/he should be looking at nix build instead of nix profile, especially since there doesn't seem to be a similar --profile option for the old-school nix-build.

@roberth
Copy link
Member

roberth commented Sep 19, 2024

Too hard IMO.
Is anything stopping us from making nix profile upgrade just work for --file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-cli Relating to the "nix" command profiles Versioned gc root symlinks; nix profile, nix-env
Projects
None yet
Development

No branches or pull requests

4 participants
@roberth @edolstra @dukc and others