All approaches @Mic92 described are indeed declarative, however you have to edit file to add new package.
Would you give up imperative nix-env --install
behavior for this? If not, then read next.
Iβve written a small (50 lines) bash script, that replaces 50% of nix-env
. Here it is https://gist.github.com/danbst/f07d9760ed15dd844e141177cf7dd478. Add an alias (declaratively in NixOS or in ~/.profile)
alias nix-env=/path/to/script
It replaces nix-env
with a thing, that supports 3 commands: install
, uninstall
and list
.
$ nix-env list
nixpkgs.htop
$ nix-env uninstall calibre
Not installed
$ nix-env install calibre
scheduled install'n'update
error: undefined variable 'calibre' at /home/danbst/.config/nixpkgs/declarative-env.nix:7:1
$ nix-env install nixpkgs.calibre
scheduled install'n'update
replacing old 'declarative-collection'
installing 'declarative-collection'
building '/nix/store/2cnhxqwxl79qj81mfkrqpl7xr361i3z1-user-environment.drv'...
created 5054 symlinks in user environment
Success
$ nix-env install '(nixpkgs.htop.overrideAttrs (_: { name = "my-override"; }))'
scheduled install'n'update
replacing old 'declarative-collection'
installing 'declarative-collection'
these derivations will be built:
/nix/store/fbggzpbs91ryysmdzc131vvkg8vm14yz-declarative-collection.drv
building '/nix/store/fbggzpbs91ryysmdzc131vvkg8vm14yz-declarative-collection.drv'...
building '/nix/store/wpb2w7vj3cq4lsl1syac7lhndahqc64r-user-environment.drv'...
created 5054 symlinks in user environment
Success
$ nix-env list
nixpkgs.calibre
(nixpkgs.htop.overrideAttrs (_: { name = "my-override"; }))
# Original nix-env is available as \nix-env
There are 3 distinctions from nix-env
here (besides completely new CLI):
- packages are identified by their attribute, instead of βnameβ. Just like in nix 2.0
- it saves itβs installed packages state in simple format: line per package (original nix-env builds a
maniphest.nix
with lots of metadata, but most importantly, it doesnβt save attribute name in maniphest)
- as a buildEnv wrapper, it maintains consistency among all installed pacakges (can upgrade/downgrade some unrelated package). Same behavior as for
nixos-rebuild
In fact, nix-env list
is an alias to cat ~/.config/nixpkgs/declarative
. This list is used to build env:
$ cat /home/danbst/.config/nixpkgs/declarative-env.nix
ββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β File: /home/danbst/.config/nixpkgs/declarative-env.nix
ββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1 β let nixos = import <nixos> { }; in
2 β let nixpkgs = import <nixpkgs> { }; in
3 β let _pkgs = import <nixpkgs> { }; in
4 β rec { _paths = [
5 β nixpkgs.calibre
6 β (nixpkgs.htop.overrideAttrs (_: { name = "my-override"; }))
7 β ];
8 β env = _pkgs.buildEnv {
9 β name = ''declarative-collection'';
10 β paths = _paths;
11 β }; }
12 β # Updated successfully!
ββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
And installed atomically with original nix-env
. Pretty much declarativeβnβimperative now! (I have a feeling that people have done this beforeβ¦)