# Best way to do declarative package management

**URL:** https://discourse.nixos.org/t/best-way-to-do-declarative-package-management/7920
**Category:** Help
**Created:** [July 1, 2020, 4:49pm UTC](https://discourse.nixos.org/t/best-way-to-do-declarative-package-management/7920 "2020-07-01T16:49:33Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![daf](https://discourse.nixos.org/user_avatar/discourse.nixos.org/daf/32/1309_2.png) [@daf](https://discourse.nixos.org/u/daf)
#### Post date: [July 1, 2020, 4:49pm UTC](https://discourse.nixos.org/t/best-way-to-do-declarative-package-management/7920/1 "2020-07-01T16:49:33Z")

</div>

With guix I am able to manage packages with a manifest file like this:

```auto
(specifications->manifest
  '("git-minimal"
    "font-hack"))

```

And then run `guix pull --manifest=/path/to/file` to add/remove/update packages.

I am looking for something similar with nix.

I found [this related thread](https://discourse.nixos.org/t/declarative-package-management-for-normal-users/1823) which shows roughly 5 different ways to do declarative package management per-user:

1. attrset
2. buildenv (packageOverrides/overlays)
3. home-manager
4. [this bash script](https://gist.github.com/danbst/f07d9760ed15dd844e141177cf7dd478)
5. nix-env -irf /path/to/file

I need a method which will work on linux distrubutions other than nixos (I’m currently on artix). Also, I plan on using the same method as root to manage global packages.

Which will work for my use case and what are the tradeoffs?

---

<div class="post-metadata">

### Author: ![jonringer](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jonringer/32/1348_2.png) [@jonringer](https://discourse.nixos.org/u/jonringer)
#### Post date: [July 1, 2020, 5:14pm UTC](https://discourse.nixos.org/t/best-way-to-do-declarative-package-management/7920/2 "2020-07-01T17:14:23Z")

</div>

> [@daf](#):
>
> Also, I plan on using the same method as root to manage global packages.

Only method I’m aware of is to use NixOS with configuration.nix, which will configure the system as a whole.

> [@daf](#):
>
> `guix pull --manifest=/path/to/file`

This looks like a user command, and I assume you probably had to run this per-user to get the updates. This would be similar to home-manager, in which you would do:

```auto
home-manager -f=/path/to/home.nix switch

```

to get a user to have all the same packages.

> [@daf](#):
>
> nix-env -irf /path/to/file

I would highly recommend not using nix-env to manage per-package installs. I’ve never used it to install a set of packages though.

---

<div class="post-metadata">

### Author: ![daf](https://discourse.nixos.org/user_avatar/discourse.nixos.org/daf/32/1309_2.png) [@daf](https://discourse.nixos.org/u/daf)
#### Post date: [July 1, 2020, 5:34pm UTC](https://discourse.nixos.org/t/best-way-to-do-declarative-package-management/7920/3 "2020-07-01T17:34:55Z")

</div>

> Only method I’m aware of is to use NixOS

Packages root installs are global on any multi-user installation, at least according to my understanding of the [cheatsheet](https://nixos.wiki/wiki/Cheatsheet).

> Packages root installs are system-wide. It does so through /etc/nixos/configuration.nix. **If root installs packages the same way users do, through ~/.nixpkgs/config.nix, they are also global**. Root’s default profile is the system-wide default profile.

I’m not on NixOS. If this weren’t true nix would be a bit less useful for me.

Anyway, I’m still in the dark. Which of these methods work for my use-case, and what are the tradeoffs?

My use case:

1. I’m not on NixOS
2. I plan on using the same method to manage global packages

---

<div class="post-metadata">

### Author: ![lheckemann](https://discourse.nixos.org/user_avatar/discourse.nixos.org/lheckemann/32/415_2.png) [@lheckemann](https://discourse.nixos.org/u/lheckemann)
#### Post date: [July 1, 2020, 8:03pm UTC](https://discourse.nixos.org/t/best-way-to-do-declarative-package-management/7920/4 "2020-07-01T20:03:54Z")

</div>

> [@daf](#):
>
> Packages root installs are global on any multi-user installation, at least according to my understanding of the [cheatsheet](https://nixos.wiki/wiki/Cheatsheet).

Correct 🙂

1 and 5 are the same thing. I’m not sure home-manager can be used as root to affect all users (@rycee can probably confirm/correct!). [Expression for a buildEnv-based declarative user environment. · GitHub](https://gist.github.com/lheckemann/402e61e8e53f136f239ecd8c17ab1deb), where I describe the buildEnv approach, also contains a short feature comparison between the two nix-env approaches and home-manager.

---

<div class="post-metadata">

### Author: ![daf](https://discourse.nixos.org/user_avatar/discourse.nixos.org/daf/32/1309_2.png) [@daf](https://discourse.nixos.org/u/daf)
#### Post date: [July 2, 2020, 4:19am UTC](https://discourse.nixos.org/t/best-way-to-do-declarative-package-management/7920/5 "2020-07-02T04:19:23Z")

</div>

Thanks. That helps a lot.

I’m going to try the buildEnv approach, then the attrset approach if I don’t like it for some reason.

---

<div class="post-metadata">

### Author: ![daf](https://discourse.nixos.org/user_avatar/discourse.nixos.org/daf/32/1309_2.png) [@daf](https://discourse.nixos.org/u/daf)
#### Post date: [July 2, 2020, 4:53am UTC](https://discourse.nixos.org/t/best-way-to-do-declarative-package-management/7920/6 "2020-07-02T04:53:42Z")

</div>

I like buildEnv after playing around with it a bit. It’s simple and works just how I wanted.

I just wish I had renamed the script to `nix-update-profile` or `update-nix-profile` so it would be less ambiguous.

EDIT: spoke too soon. This kills query functionality for nix-env. Going to try the attrset approach.

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [July 2, 2020, 8:24am UTC](https://discourse.nixos.org/t/best-way-to-do-declarative-package-management/7920/7 "2020-07-02T08:24:36Z")

</div>

What do you mean by “query functionality of nix-env”?

---

<div class="post-metadata">

### Author: ![daf](https://discourse.nixos.org/user_avatar/discourse.nixos.org/daf/32/1309_2.png) [@daf](https://discourse.nixos.org/u/daf)
#### Post date: [July 2, 2020, 6:58pm UTC](https://discourse.nixos.org/t/best-way-to-do-declarative-package-management/7920/8 "2020-07-02T18:58:10Z")

</div>

buildEnv writes `manifest.nix` to your `.nix-profile`, which does this:

```auto
        throw ''
          Your user environment is a buildEnv which is incompatible with
          nix-envs built-in env builder. Edit your home expression and run
          update-profile instead!
        ''

```

It’s designed to lock you into buildEnv’s way of package management, but it’s a bit too indiscriminate for my liking, as queries no longer work.

![image](https://discourse.nixos.org/uploads/default/original/2X/4/48707dfe0880d93ddcb93e96ae5159915a88e336.png)

EDIT: `nix search ...` still works, though. I’ll stick with it for now.

---

<div class="post-metadata">

### Author: ![rycee](https://discourse.nixos.org/user_avatar/discourse.nixos.org/rycee/32/219_2.png) [@rycee](https://discourse.nixos.org/u/rycee)
#### Post date: [July 10, 2020, 4:19pm UTC](https://discourse.nixos.org/t/best-way-to-do-declarative-package-management/7920/9 "2020-07-10T16:19:11Z")

</div>

> [@lheckemann](#):
>
> 1 and 5 are the same thing. I’m not sure home-manager can be used as root to affect all users (@rycee can probably confirm/correct!).

Not sure, I’ve never tried it 🙂

---

<div class="post-metadata">

### Author: ![lilyball](https://discourse.nixos.org/user_avatar/discourse.nixos.org/lilyball/32/939_2.png) [@lilyball](https://discourse.nixos.org/u/lilyball)
#### Post date: [July 13, 2020, 12:25am UTC](https://discourse.nixos.org/t/best-way-to-do-declarative-package-management/7920/10 "2020-07-13T00:25:03Z")

</div>

The [attrset overlay approach](https://gist.github.com/LnL7/570349866bb69467d0caf5cb175faa74#gistcomment-3372828) has worked well for me for a long time. And it’s convenient to be able to do once-off `nix-env -iA` commands, in particular if one of my current packages is temporarily broken and can’t update (and therefore I can’t rebuild my whole environment), but I need to install or update a separate one.
