Skip to main content

NixOS: Custom Packages

NixOS - This article is part of a series.
Part 13: This Article

Writing custom packages
#

Note

We can use flake inputs for sources which don’t have versioned download links. Their hashes our updated by running the nix flake update command.

For software that is not included in the official Nix repository we can make use of Nix derivations to package our own applications:

{
  src,
  lib,
  stdenv,
  fetchurl,
  autoPatchelfHook,
  gnutar,
  wrapGAppsHook3,
  glibc,
  gtk3,
  makeDesktopItem,
}:

let
  pname = "vuescan";
  version = "9.8";
  desktopItem = makeDesktopItem {
    name = "VueScan";
    desktopName = "VueScan";
    genericName = "Scanning Program";
    comment = "Scanning Program";
    icon = "vuescan";
    terminal = false;
    type = "Application";
    startupNotify = true;
    categories = [
      "Graphics"
      "Utility"
    ];
    keywords = [
      "scan"
      "scanner"
    ];
    exec = "vuescan";
  };
in
stdenv.mkDerivation rec {
  name = "${pname}-${version}";

  inherit src;

  nativeBuildInputs = [
    autoPatchelfHook
    gnutar
    wrapGAppsHook3
  ];
  buildInputs = [
    glibc
    gtk3
  ];

  dontStrip = true;

  installPhase = ''
    install -m755 -D vuescan $out/bin/vuescan
    mkdir -p $out/share/icons/hicolor/scalable/apps/
    mkdir -p $out/lib/udev/rules.d/
    mkdir -p $out/share/applications/
    cp vuescan.svg $out/share/icons/hicolor/scalable/apps/vuescan.svg
    cp vuescan.rul $out/lib/udev/rules.d/60-vuescan.rules
    ln -s ${desktopItem}/share/applications/* $out/share/applications
    runHook postInstall
  '';

  meta = with lib; {
    homepage = "https://www.hamrick.com/about-vuescan.html";
    description = "Scanning software for film scanners";
    license = licenses.unfree;
    platforms = [ "x86_64-linux" ];
  };
}

Importing derivations
#

Custom package derivations can be imported into a nix configuration using overlays:

  flake.overlays.additional-packages = final: prev: {
    vuescan = final.callPackage ../../packages/vuescan.nix { src = inputs.vuescan-source; };
    epson-v550-plugin = final.callPackage ../../packages/epson-v550-plugin.nix { };
    cosmic-ext-applet-clipboard-manager =
      final.callPackage ../../packages/cosmic-ext-applet-clipboard-manager.nix
        { };
  };

NixOS - This article is part of a series.
Part 13: This Article