8

So I've installed Nix on Arch linux and I'm able to run nix-env -i example, however how can I define a Nix configuration?

As I don't have any /etc/nixos/configuration.nix file present.

Is this possible?


My goal here is to be able to define a configuration which I could then use something like nixos-rebuild switch to install and provision all the software.

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
  • Relevant: [How to configure a Nix environment, outside of NixOS?](https://unix.stackexchange.com/questions/369234/how-to-configure-a-nix-environment-outside-of-nixos) and the [`rycee/home-manager`](https://github.com/rycee/home-manager) tool. – toraritte Oct 18 '19 at 16:32

2 Answers2

8

I use NixOS, but I use /etc/nixos/configuration.nix to describe my system; I keep it fairly minimal and prefer not to install "user" software by editing configuration.nix.

So what I do instead is use my ~/.nixpkgs/config.nix (which I believe you also have an equivalent of even in a non-NixOS nix install? I've never actually used nix separately).

The basic structure I use is this:

{
  packageOverrides = nixpkgs: with nixpkgs; rec {
    mine = with pkgs; buildEnv {
      name = "mine";
      paths = [
        # your packages here
      ];
    };
  };
}

buildEnv is a convenience function from nix that makes an "environment" package out of a bunch of others; installing the package mine depends on (and so installs) all of the things listed in paths, and also makes sure they get included in PATH and things like that.

Then I just use nix-env -riA nixos.mine to deploy changes I've made to my environment description (or to rebuild my environment following channel updates). The -r tells it to remove everything else other than mine from the new generation of my profile, which means I can (ab?)use nix-env -i some-package as a way of "temporarily" installing some-package, and if I don't decide I like it enough to actually record it in my config.nix it'll just get removed anyway next time I deploy.

Ben
  • 68,572
  • 20
  • 126
  • 174
2

You can certainly create your own configuration. For example, you can do something like this:

let
    pkgs = import <nixpkgs> {};
in
    {    
        packages = [
            pkgs.vim
            pkgs.gimp
        ];
    }

That would produce a set containing an attribute called packages, containing a list of Nix packages. But, you'd need to develop a tool to build environments from this, which is part of what nix-env does. For example, the tool can use nix-env to determine what is already installed, compare that to the selected packages in the configuration, and then install/uninstall packages accordingly.

You don't have a /etc/nixos/configuration.nix because that's NixOS-specific. So while you can do as you asked, you'd have to roll your own solution.

Emmanuel Rosa
  • 9,697
  • 2
  • 14
  • 20
  • I suppose if I didn't use any services I could mount a Nixos filesystem and chroot into that? I might try to do this in a Docker container otherwise. – Chris Stryczynski Jul 31 '17 at 16:52
  • Probably... but that's getting awful complicated. If all you want to do is install packages in-mass, you can feed them all into `nix-env` at once, and then use it's upgrade feature to upgrade all the packages at once. That way, a rollback would undo upgrading the entire set of packages, like on NixOS. If Arch Linux is set up to use systemd, then you can "boot" it within a systemd-nspawn container from NixOS (or any other systemd-equipped OS). – Emmanuel Rosa Aug 01 '17 at 01:02