From 56a7b4b4005ae8bed2d1946daceff5ffa9f23b06 Mon Sep 17 00:00:00 2001 From: Daste Date: Thu, 27 Aug 2026 00:09:48 +0200 Subject: [PATCH] nixos/doc: Improve the configuration/adding-custom-packages chapter --- .../adding-custom-packages.section.md | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/nixos/doc/manual/configuration/adding-custom-packages.section.md b/nixos/doc/manual/configuration/adding-custom-packages.section.md index 221f4ad526cc..697eb5e8b387 100644 --- a/nixos/doc/manual/configuration/adding-custom-packages.section.md +++ b/nixos/doc/manual/configuration/adding-custom-packages.section.md @@ -41,15 +41,14 @@ tree. For instance, here is how you specify a build of the { environment.systemPackages = let - my-hello = - with pkgs; - stdenv.mkDerivation rec { - name = "hello-2.8"; - src = fetchurl { - url = "mirror://gnu/hello/${name}.tar.gz"; - hash = "sha256-5rd/gffPfa761Kn1tl3myunD8TuM+66oy1O7XqVGDXM="; - }; + my-hello = pkgs.stdenv.mkDerivation (finalAttrs: { + pname = "hello"; + version = "2.8"; + src = pkgs.fetchurl { + url = "mirror://gnu/hello/hello-${finalAttrs.version}.tar.gz"; + hash = "sha256-5rd/gffPfa761Kn1tl3myunD8TuM+66oy1O7XqVGDXM="; }; + }); in [ my-hello ]; } @@ -59,27 +58,28 @@ Of course, you can also move the definition of `my-hello` into a separate Nix expression, e.g. ```nix -{ environment.systemPackages = [ (import ./my-hello.nix) ]; } +{ environment.systemPackages = [ (pkgs.callPackage ./my-hello.nix { }) ]; } ``` where `my-hello.nix` contains: ```nix -with import { }; # bring all of Nixpkgs into scope +{ stdenv, fetchurl }: # declare dependencies as arguments -stdenv.mkDerivation rec { - name = "hello-2.8"; +stdenv.mkDerivation (finalAttrs: { + pname = "hello"; + version = "2.8"; src = fetchurl { - url = "mirror://gnu/hello/${name}.tar.gz"; + url = "mirror://gnu/hello/hello-${finalAttrs.version}.tar.gz"; hash = "sha256-5rd/gffPfa761Kn1tl3myunD8TuM+66oy1O7XqVGDXM="; }; -} +}) ``` -This allows testing the package easily: +This allows testing the package without building a whole system configuration: ```ShellSession -$ nix-build my-hello.nix +$ nix-build -E 'with import { }; callPackage ./my-hello.nix { }' $ ./result/bin/hello Hello, world! ```