Skip to content

Latest commit

 

History

History
168 lines (115 loc) · 3.27 KB

20-overlays-and-flakes.org

File metadata and controls

168 lines (115 loc) · 3.27 KB

Overlays & Flakes

Problem!


I want to override a package in nixpkgs, and ship this override to other developers or users.

How?

Overlays!


  • Provide package changes
  • “Overlayed” onto an existing package source (e.g. nixpkgs)
  • Allow for local, or organisation-wide overrides
  • Implemented as a function
self: super: {
  htop = self.callPackage ./patches/htop { inherit (super) htop; };
}

Overlay structure


file:imgs/overlays1.png

Situation: no overlays


(enjoy some drawings from my eInk tablet)

file:imgs/overlays2.png

Situation: one overlay


file:imgs/overlays3.png

Situation: two overlays


file:imgs/overlays4.png

  • self is sometimes also called final
  • super is sometimes also called prev

self: super: {
  htop = builtins.trace "Overriding the htop package" super.htop;
}

Overlays to create packages


Useful for personal or organisation-wide packages.

self: super: {
  my-hello = self.writeShellScript "my-hello" ''
    ${self.bash}/bin/bash
    ${self.hello}/bin/hello | ${self.cowsay}/bin/cowsay
  '';
}

 ❤ (theia) s/overlays> tree patches
patches/
└── htop
    ├── 0001-htop-untruncated-username.patch
    └── default.nix     
self: super: {
  htop = self.callPackage ./patches/htop { inherit (super) htop; };
}     

Quiz: why do we inherit from super to pass htop?

  • Don’t define a new package, override parts of the existing one
  • Include as many other depedencies as you need
  • Then include patches, or change build steps
{ htop }:
htop.overrideAtts ({ patches ? [], ... }: {
  patches = patches ++ [ ./0001-htop-untruncated-username.patch ];
})

How do you use an Overlay?

nixpkgs-overlays PATH key


  • Required to make nix-shell use the overlay
  • Means the overlay needs to stick around at runtime
    • and things break if you move/ delete it!


export NIX_PATH="$NIX_PATH:nixpkgs-overlays=/path/to/overlay"

Nixpkgs initialisation parameter


  • When loading <nixpkgs> you can provide various parameters
  • One of them is the overlays key, which accepts a list
  • Alternatively: define load-nixpkgs.nix, and load that instead
# load-nixpkgs.nix
{ overlays ? [], ... } @ args:
import <nixpkgs> (args // {
  overlays = overlays ++ [ (import ./overlay) ];
});
❤ (theia) ~> nix build -f load-nixpkgs.nix htop
... build the patched htop ...

In-line example


Create an in-line overlay to override a certain package

import <nixpkgs> ({
  overlays = [
    (self: super: {
      htop = builtins.trace "htop in the overlay" super.htop;
    })
  ];
})