Posted on March 12, 2017 by rodney
Tags: nerd, nix, emacs

If you ever wanted to run a slightly newer version of a package, you can just override its src attribute.

{ pkgs ? import <nixpkgs> {} }:
pkgs.emacs.overrideDerivation (oldAttrs: {
  src = pkgs.fetchurl {
    url = ftp://alpha.gnu.org/gnu/emacs/pretest/emacs-25.2-rc2.tar.xz;
    sha256 = "11znicdblc3dn5xl55kkw0inn5944l33m7787zygvy97nha56h2g";
  };
  patches = [];
})

This will provide an RC version of Emacs. The patches attribute is also overridden, because any patches in the base emacs derivation probably won’t apply cleanly to the newer sources.

Another Example: bcc

The BPF Compiler Collection (BCC) provides several useful tools for kernel tracing. There are some cool things in the latest master branch on GitHub, but the author hasn’t made a release for a while.

So we just override the released version with whatever source revision we are interested in:

{ pkgs ? import <nixpkgs> {} }:

let
  # needs the headers for the currently running kernel so that the BPF
  # module can be compiled.
  kernel = pkgs.linuxPackages_4_10;

  # latest release is from september
  release-0_2_0 = kernel.bcc;

  snapshot-2017-02-09 = kernel.bcc.overrideDerivation (super: {
    name = "bcc-git-2017-02-09";
    src = pkgs.fetchFromGitHub {
      owner = "iovisor";
      repo = "bcc";
      rev = "3a3b0f9804d5640f918673efb0d934d1fd931684";
      sha256 = "05nf37iwz4lk1ry4hlndgmfqgn84661wflym4ckh9cw4b7gan71y";
    };
  });

  snapshot-2017-02-23 = kernel.bcc.overrideDerivation (super: {
    name = "bcc-git-2017-02-23";
    src = pkgs.fetchFromGitHub {
      owner = "iovisor";
      repo = "bcc";
      rev = "a0752408bd9c5bf43ab25104ee559a6a89750b42";
      sha256 = "06669fnl1cs11kbpbja31z40csy0bkw05j190l1pk035p8lxnjyd";
    };
  });

in
  snapshot-2017-02-23

The result can be installed into your nix profile with:

nix-env -f bcc.nix -i 

The point is, in most cases you can re-use the existing build scripts from nixpkgs, instead of manually building the software.