Override scripts in nix derivations

1.2k views Asked by At

Is there a way to override scripts and/or configs in an existing derivation without having to recompile the whole package?

I'd like to create a new version of gnome-session with modified $out/share/gnome-session/sessions/gnome.session modified. Using overridePackage I can change the preFixup phase, but this causes the whole gnome-session package to be recompiled.

1

There are 1 answers

0
Jonas Juselius On BEST ANSWER

A simple solution to problem is creating a new derivation without any sources. The trick is to create links to every file and directory in the base derivation, except for any file(s) in need of modification. These files are handled explicitly according to needs.

The snippet below shows how to create a new gnome-session with xmonad instead of gnome-shell.

{ nixpkgs ? import <nixpkgs> {} }:
let
  inherit (nixpkgs) pkgs;  
in
  pkgs.stdenv.mkDerivation {
    name = "gnome-session";
    builder = pkgs.writeText "builder.sh" ''
      # source standard environment
      . $stdenv/setup

      # shorthands
      refpkg=${pkgs.gnome3.gnome_session}
      file=share/gnome-session/sessions/gnome.session

      # create output dirs for new derivation
      mkdir -p $out/share
      mkdir -p $out/share/gnome-session/sessions

      # link unchanged files from the original gnome-session 
      ln -sf $refpkg/bin $out
      ln -sf $refpkg/libexec $out
      find $refpkg/share -maxdepth 1 \
        -not -name gnome-session -exec ln -sf {} $out/share \;

     # change gnome-shell to xmonad
      sed 's/org.gnome.Shell/xmonad/' $refpkg/$file > $out/$file
    '';
     # make sure gnome-session is installed before deriving it!
    buildInputs = [ pkgs.gnome3.gnome_session ];
  }