Nixpkgs configuration

From wikinotes

nix, nixpkgs, and nixos are each separate systems and each have their own configuration.
This page documents the nixpkgs system that defines/exposes/overrides packages.
You may also be interested in nix configuration.


Documentation

nixpkgs library https://nixos.org/nixpkgs/manual/#chap-functions

Locations

~/.config/nixpkgs/config.nix nixpkgs config
~/.config/nixpkgs/overlays.nix overlays loaded into every nix env
~/.config/nixpkgs/overlays/*.nix overlays loaded into every nix env

Overview

nixpkgs is configured for the user, and globally.
they share configuration options, but defining an option globally does not always match the behaviour of setting it for the user.

Note that you can access your config from nixpkgs.config.

~/.config/nixpkgs/config.nix

# ~/.config/nixpkgs/config.nix

{ pkgs ? import <nixpkgs> {} }:
  {
    # allow non-free plugins (gstreamer, flash, etc.)
    # for user profiles
    allowUnfree = true;
  
    # allowlist insecure packages
    permittedInsecurePackages = [
      "hello-1.2.3"
    ];
  }


/etc/nix/configuration.nix

{ pkgs ? import <nixpkgs> {} }:
  {
    nixpkgs.config = {
  
      # allow non-free plugins (gstreamer, flash, etc.)
      # for shared profiles?
      allowUnfree = true;
    };
  }

use config.nix variables in packages


I use this to define groups of development packages that I can use in shell.nix.

config.nix

# ~/.config/nixpkgs/config.nix

{ pkgs ? import <nixpkgs> {} }:
  {
    # ...
    vim-dev-base = [ pkgs.neovim pkgs.ripgrep ];
    vim-dev-ruby2 = vim-dev-base ++ [ solargraph ];
  }

shell.nix

# shell.nix
{ pkgs ? import <nixpkgs> {} }:

let
  pkg = import ./default.nix {};
       
in
  pkg.overrideAttrs(
    old: {
      buildInputs = old.buildInputs ++ pkgs.config.vim-dev-base;
    }
  )


Package Overrides

Package overrides allow you to change the parameters passed to packages.

change package parameters

Nix pills 17 describes how graphviz builds without X support if xlibs package is not provided.
Here is the extracted example of the modified arguments.

https://nixos.org/nixos/nix-pills/nixpkgs-overriding-packages.html

# ~/.config/nixpkgs/config.nix

{
  # modify graphviz package so that it is built without X support
  packageOverrides = pkgs: rec {
    pkgs.graphviz = pkgs.graphviz.override { xlibs = null; };
  };
}

define/resolve package sets

You can also define/resolve package sets.
https://nixos.org/nixpkgs/manual/#sec-getting-documentation

# ~/.config/nixpkgs/config.nix

{
  packageOverrides = pkgs: with pkgs; {
    vim-dev = pkgs.buildEnv {
      name = "vim-dev";
      paths = [
        nvim
        rg
        rippertags
      ];
      # also install man/doc pages in addition to bin/ (if available)
      # (you could use this for lib/ dirs as well)
      pathsToLink = [ "/share/man" "/share/doc" "/bin" ];
      extraOutputsToInstall = [ "man" "doc" ];

      # NOTE: you'll also need to set MANPATH etc.
      # solution in docs seems overly complex, I need to test.
    };
  };
}
nix-env -iA nixpkgs.vim-dev  # nix env with these packages

Overlays

Overlays were built to replace package overrides.
They provide a layered/composable way of applying overrides.

See http://blog.tpleyer.de/posts/2020-01-29-Nix-overlay-evaluation-example.html

Override package attributes


The following is loaded and applied automatically every time the chromium package is built.

# ~/.config/nixpkgs/overlays/chromium.nix

# self refers to expression being defined.
# super refers expressions belonging to system

self: super: {
  chromium = super.chromium.override {
    enableNaCl = true;
  };
}

See https://ebzzry.io/en/nix/#overlays

Define private/non-nixpkgs package


This is a neat trick for dynamically generating packages.

Overlay exposes the package.

# ~/.config/nixpkgs/overlays/foo.nix

self: super: {
  foo = super.callPackage ./pkgs/foo { };
}

Package definition

# ~/.config/nixpkgs/overlays/pkgs/foo/default.nix

{ stdenv, vim, ripgrep }:

stdenv.mkDerivation rec {
  name = "foo-${version}";
  version = "0.0.1";
  buildInputs = [ vim ripgrep ];
  # ... rest of package definition ...
}
nix-env -iA nixpkgs.foo

See https://ebzzry.io/en/nix/#overlays