Declarative Specification of a User Profile with the Nix Package Manager

Nix is a package manager that lets your install software into a separate user profile and a system profile. The system profile is available for users of NixOS, and the installed software is specified in the /etc/nixos/configuration.nix file. For those on distributions other than NixOS, software can only be installed imperatively(via the command line instead of a configuration file) into the user profile. With a small trick, we can specify our user profile declaratively instead of imperatively.

The general idea is to create a custom nix package, with our desired profile as a dependency. To pull in our desired applications, all we need to do is install our single custom package.

First create a configuration file for the package tree at ~/.nixpkgs/config.nix. The basic layout should look like:

{  packageOverrides = pkgs: with pkgs; rec {
    myProfile = stdenv.mkDerivation rec {
      name = "my-profile";

      buildInputs = [
        # Put your desired packages here
      ];

      buildCommand = with stdenv.lib.strings; ''
        mkdir -p $out/bin;
        cd $out/bin;
        ${concatStringsSep "\n"
          (map (p: "if [ -d ${p}/bin ]; then cp ${p}/bin/* .; fi") buildInputs)
        }
      '';
      phases = "";
    };

This will add a new package at myProfile in the package tree with a name of "my-profile". The package works by copying the binaries from the source package, making them available when the package is installed.

You can install the package by running nix-env -iA myProfile.

Comments

There are currently no comments

New Comment

required

required (not published)

optional