doc: use finalAttrs pattern

This commit is contained in:
Pol Dellaiera 2025-04-19 22:09:31 +02:00
parent 47f000d991
commit b4515ff6c2
8 changed files with 57 additions and 55 deletions

View File

@ -23,15 +23,15 @@ In Nixpkgs, `cargo-tauri.hook` overrides the default build and install phases.
wrapGAppsHook4, wrapGAppsHook4,
}: }:
rustPlatform.buildRustPackage rec { rustPlatform.buildRustPackage (finalAttrs: {
# . . . # ...
useFetchCargoVendor = true; useFetchCargoVendor = true;
cargoHash = "..."; cargoHash = "...";
# Assuming our app's frontend uses `npm` as a package manager # Assuming our app's frontend uses `npm` as a package manager
npmDeps = fetchNpmDeps { npmDeps = fetchNpmDeps {
name = "${pname}-npm-deps-${version}"; name = "${finalAttrs.pname}-npm-deps-${finalAttrs.version}";
inherit src; inherit src;
hash = "..."; hash = "...";
}; };
@ -61,8 +61,8 @@ rustPlatform.buildRustPackage rec {
# And make sure we build there too # And make sure we build there too
buildAndTestSubdir = cargoRoot; buildAndTestSubdir = cargoRoot;
# . . . # ...
} })
``` ```
## Variables controlling cargo-tauri {#tauri-hook-variables-controlling} ## Variables controlling cargo-tauri {#tauri-hook-variables-controlling}

View File

@ -13,14 +13,14 @@ The following is an example expression using `buildGoModule`:
```nix ```nix
{ {
pet = buildGoModule rec { pet = buildGoModule (finalAttrs: {
pname = "pet"; pname = "pet";
version = "0.3.4"; version = "0.3.4";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "knqyf263"; owner = "knqyf263";
repo = "pet"; repo = "pet";
rev = "v${version}"; rev = "v${finalAttrs.version}";
hash = "sha256-Gjw1dRrgM8D3G7v6WIM2+50r4HmTXvx0Xxme2fH9TlQ="; hash = "sha256-Gjw1dRrgM8D3G7v6WIM2+50r4HmTXvx0Xxme2fH9TlQ=";
}; };
@ -32,7 +32,7 @@ The following is an example expression using `buildGoModule`:
license = lib.licenses.mit; license = lib.licenses.mit;
maintainers = with lib.maintainers; [ kalbasit ]; maintainers = with lib.maintainers; [ kalbasit ];
}; };
}; });
} }
``` ```

View File

@ -198,14 +198,14 @@ Here's an example:
fetchFromGitHub, fetchFromGitHub,
}: }:
buildNpmPackage rec { buildNpmPackage (finalAttrs: {
pname = "flood"; pname = "flood";
version = "4.7.0"; version = "4.7.0";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "jesec"; owner = "jesec";
repo = pname; repo = pname;
rev = "v${version}"; rev = "v${finalAttrs.version}";
hash = "sha256-BR+ZGkBBfd0dSQqAvujsbgsEPFYw/ThrylxUbOksYxM="; hash = "sha256-BR+ZGkBBfd0dSQqAvujsbgsEPFYw/ThrylxUbOksYxM=";
}; };
@ -222,7 +222,7 @@ buildNpmPackage rec {
license = lib.licenses.gpl3Only; license = lib.licenses.gpl3Only;
maintainers = with lib.maintainers; [ winter ]; maintainers = with lib.maintainers; [ winter ];
}; };
} })
``` ```
In the default `installPhase` set by `buildNpmPackage`, it uses `npm pack --json --dry-run` to decide what files to install in `$out/lib/node_modules/$name/`, where `$name` is the `name` string defined in the package's `package.json`. In the default `installPhase` set by `buildNpmPackage`, it uses `npm pack --json --dry-run` to decide what files to install in `$out/lib/node_modules/$name/`, where `$name` is the `name` string defined in the package's `package.json`.

View File

@ -370,7 +370,7 @@ let
# pick a repository derivation, here we will use buildMaven # pick a repository derivation, here we will use buildMaven
repository = callPackage ./build-maven-repository.nix { }; repository = callPackage ./build-maven-repository.nix { };
in in
stdenv.mkDerivation rec { stdenv.mkDerivation (finalAttrs: {
pname = "maven-demo"; pname = "maven-demo";
version = "1.0"; version = "1.0";
@ -393,7 +393,7 @@ stdenv.mkDerivation rec {
runHook postInstall runHook postInstall
''; '';
} })
``` ```
::: {.tip} ::: {.tip}
@ -441,7 +441,7 @@ We make sure to provide this classpath to the `makeWrapper`.
let let
repository = callPackage ./build-maven-repository.nix { }; repository = callPackage ./build-maven-repository.nix { };
in in
stdenv.mkDerivation rec { stdenv.mkDerivation (finalAttrs: {
pname = "maven-demo"; pname = "maven-demo";
version = "1.0"; version = "1.0";
@ -464,16 +464,16 @@ stdenv.mkDerivation rec {
mkdir -p $out/bin mkdir -p $out/bin
classpath=$(find ${repository} -name "*.jar" -printf ':%h/%f'); classpath=$(find ${repository} -name "*.jar" -printf ':%h/%f');
install -Dm644 target/${pname}-${version}.jar $out/share/java install -Dm644 target/maven-demo-${finalAttrs.version}.jar $out/share/java
# create a wrapper that will automatically set the classpath # create a wrapper that will automatically set the classpath
# this should be the paths from the dependency derivation # this should be the paths from the dependency derivation
makeWrapper ${jre}/bin/java $out/bin/${pname} \ makeWrapper ${jre}/bin/java $out/bin/maven-demo \
--add-flags "-classpath $out/share/java/${pname}-${version}.jar:''${classpath#:}" \ --add-flags "-classpath $out/share/java/maven-demo-${finalAttrs.version}.jar:''${classpath#:}" \
--add-flags "Main" --add-flags "Main"
runHook postInstall runHook postInstall
''; '';
} })
``` ```
#### MANIFEST file via Maven Plugin {#manifest-file-via-maven-plugin} #### MANIFEST file via Maven Plugin {#manifest-file-via-maven-plugin}
@ -534,7 +534,7 @@ let
# pick a repository derivation, here we will use buildMaven # pick a repository derivation, here we will use buildMaven
repository = callPackage ./build-maven-repository.nix { }; repository = callPackage ./build-maven-repository.nix { };
in in
stdenv.mkDerivation rec { stdenv.mkDerivation (finalAttrs: {
pname = "maven-demo"; pname = "maven-demo";
version = "1.0"; version = "1.0";
@ -559,15 +559,15 @@ stdenv.mkDerivation rec {
# create a symbolic link for the repository directory # create a symbolic link for the repository directory
ln -s ${repository} $out/repository ln -s ${repository} $out/repository
install -Dm644 target/${pname}-${version}.jar $out/share/java install -Dm644 target/maven-demo-${finalAttrs.version}.jar $out/share/java
# create a wrapper that will automatically set the classpath # create a wrapper that will automatically set the classpath
# this should be the paths from the dependency derivation # this should be the paths from the dependency derivation
makeWrapper ${jre}/bin/java $out/bin/${pname} \ makeWrapper ${jre}/bin/java $out/bin/maven-demo \
--add-flags "-jar $out/share/java/${pname}-${version}.jar" --add-flags "-jar $out/share/java/maven-demo-${finalAttrs.version}.jar"
runHook postInstall runHook postInstall
''; '';
} })
``` ```
::: {.note} ::: {.note}
Our script produces a dependency on `jre` rather than `jdk` to restrict the runtime closure necessary to run the application. Our script produces a dependency on `jre` rather than `jdk` to restrict the runtime closure necessary to run the application.

View File

@ -28,14 +28,14 @@ Rust applications are packaged by using the `buildRustPackage` helper from `rust
rustPlatform, rustPlatform,
}: }:
rustPlatform.buildRustPackage rec { rustPlatform.buildRustPackage (finalAttrs: {
pname = "ripgrep"; pname = "ripgrep";
version = "14.1.1"; version = "14.1.1";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "BurntSushi"; owner = "BurntSushi";
repo = pname; repo = "ripgrep";
rev = version; tag = finalAttrs.version;
hash = "sha256-gyWnahj1A+iXUQlQ1O1H1u7K5euYQOld9qWm99Vjaeg="; hash = "sha256-gyWnahj1A+iXUQlQ1O1H1u7K5euYQOld9qWm99Vjaeg=";
}; };
@ -48,7 +48,7 @@ rustPlatform.buildRustPackage rec {
license = lib.licenses.unlicense; license = lib.licenses.unlicense;
maintainers = [ ]; maintainers = [ ];
}; };
} })
``` ```
`buildRustPackage` requires a `cargoHash` attribute, computed over all crate sources of this package. `buildRustPackage` requires a `cargoHash` attribute, computed over all crate sources of this package.
@ -104,21 +104,21 @@ be made invariant to the version by setting `cargoDepsName` to
`pname`: `pname`:
```nix ```nix
rustPlatform.buildRustPackage rec { rustPlatform.buildRustPackage (finalAttrs: {
pname = "broot"; pname = "broot";
version = "1.2.0"; version = "1.2.0";
src = fetchCrate { src = fetchCrate {
inherit pname version; inherit (finalAttrs) pname version;
hash = "sha256-aDQA4A5mScX9or3Lyiv/5GyAehidnpKKE0grhbP1Ctc="; hash = "sha256-aDQA4A5mScX9or3Lyiv/5GyAehidnpKKE0grhbP1Ctc=";
}; };
useFetchCargoVendor = true; useFetchCargoVendor = true;
cargoHash = "sha256-iDYh52rj1M5Uupvbx2WeDd/jvQZ+2A50V5rp5e2t7q4="; cargoHash = "sha256-iDYh52rj1M5Uupvbx2WeDd/jvQZ+2A50V5rp5e2t7q4=";
cargoDepsName = pname; cargoDepsName = finalAttrs.pname;
# ... # ...
} })
``` ```
### Importing a `Cargo.lock` file {#importing-a-cargo.lock-file} ### Importing a `Cargo.lock` file {#importing-a-cargo.lock-file}
@ -184,7 +184,7 @@ The output hash of each dependency that uses a git source must be
specified in the `outputHashes` attribute. For example: specified in the `outputHashes` attribute. For example:
```nix ```nix
rustPlatform.buildRustPackage rec { rustPlatform.buildRustPackage {
pname = "myproject"; pname = "myproject";
version = "1.0.0"; version = "1.0.0";
@ -209,7 +209,7 @@ For usage outside nixpkgs, `allowBuiltinFetchGit` could be used to
avoid having to specify `outputHashes`. For example: avoid having to specify `outputHashes`. For example:
```nix ```nix
rustPlatform.buildRustPackage rec { rustPlatform.buildRustPackage {
pname = "myproject"; pname = "myproject";
version = "1.0.0"; version = "1.0.0";
@ -235,7 +235,7 @@ If you want to use different features for check phase, you can use
For example: For example:
```nix ```nix
rustPlatform.buildRustPackage rec { rustPlatform.buildRustPackage {
pname = "myproject"; pname = "myproject";
version = "1.0.0"; version = "1.0.0";
@ -427,7 +427,7 @@ source code in a reproducible way. If it is missing or out-of-date one can use
the `cargoPatches` attribute to update or add it. the `cargoPatches` attribute to update or add it.
```nix ```nix
rustPlatform.buildRustPackage rec { rustPlatform.buildRustPackage {
# ... # ...
cargoPatches = [ cargoPatches = [
# a patch file to add/update Cargo.lock in the source code # a patch file to add/update Cargo.lock in the source code
@ -705,7 +705,7 @@ Some projects, especially GNOME applications, are built with the Meson Build Sys
tinysparql, tinysparql,
}: }:
stdenv.mkDerivation rec { stdenv.mkDerivation (finalAttrs: {
pname = "health"; pname = "health";
version = "0.95.0"; version = "0.95.0";
@ -713,12 +713,12 @@ stdenv.mkDerivation rec {
domain = "gitlab.gnome.org"; domain = "gitlab.gnome.org";
owner = "World"; owner = "World";
repo = "health"; repo = "health";
rev = version; tag = finalAttrs.version;
hash = "sha256-PrNPprSS98yN8b8yw2G6hzTSaoE65VbsM3q7FVB4mds="; hash = "sha256-PrNPprSS98yN8b8yw2G6hzTSaoE65VbsM3q7FVB4mds=";
}; };
cargoDeps = rustPlatform.fetchCargoVendor { cargoDeps = rustPlatform.fetchCargoVendor {
inherit pname version src; inherit (finalAttrs) pname version src;
hash = "sha256-eR1ZGtTZQNhofFUEjI7IX16sMKPJmAl7aIFfPJukecg="; hash = "sha256-eR1ZGtTZQNhofFUEjI7IX16sMKPJmAl7aIFfPJukecg=";
}; };
@ -740,7 +740,7 @@ stdenv.mkDerivation rec {
]; ];
# ... # ...
} })
``` ```
## `buildRustCrate`: Compiling Rust crates using Nix instead of Cargo {#compiling-rust-crates-using-nix-instead-of-cargo} ## `buildRustCrate`: Compiling Rust crates using Nix instead of Cargo {#compiling-rust-crates-using-nix-instead-of-cargo}
@ -1000,20 +1000,21 @@ let
}; };
in in
rustPlatform.buildRustPackage rec { rustPlatform.buildRustPackage (finalAttrs: {
pname = "ripgrep"; pname = "ripgrep";
version = "14.1.1"; version = "14.1.1";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "BurntSushi"; owner = "BurntSushi";
repo = "ripgrep"; repo = "ripgrep";
rev = version; tag = finalAttrs.version;
hash = "sha256-gyWnahj1A+iXUQlQ1O1H1u7K5euYQOld9qWm99Vjaeg="; hash = "sha256-gyWnahj1A+iXUQlQ1O1H1u7K5euYQOld9qWm99Vjaeg=";
}; };
useFetchCargoVendor = true; useFetchCargoVendor = true;
cargoHash = "sha256-9atn5qyBDy4P6iUoHFhg+TV6Ur71fiah4oTJbBMeEy4="; cargoHash = "sha256-9atn5qyBDy4P6iUoHFhg+TV6Ur71fiah4oTJbBMeEy4=";
# Tests require network access. Skipping.
doCheck = false; doCheck = false;
meta = { meta = {
@ -1025,7 +1026,7 @@ rustPlatform.buildRustPackage rec {
]; ];
maintainers = with lib.maintainers; [ ]; maintainers = with lib.maintainers; [ ];
}; };
} })
``` ```
Follow the below steps to try that snippet. Follow the below steps to try that snippet.

View File

@ -82,15 +82,15 @@ let
generated = swiftpm2nix.helpers ./nix; generated = swiftpm2nix.helpers ./nix;
in in
stdenv.mkDerivation rec { stdenv.mkDerivation (finalAttrs: {
pname = "myproject"; pname = "myproject";
version = "0.0.0"; version = "0.0.0";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "nixos"; owner = "nixos";
repo = pname; repo = "myproject";
rev = version; tag = finalAttrs.version;
hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="; hash = "";
}; };
# Including SwiftPM as a nativeBuildInput provides a buildPhase for you. # Including SwiftPM as a nativeBuildInput provides a buildPhase for you.
@ -117,7 +117,7 @@ stdenv.mkDerivation rec {
runHook postInstall runHook postInstall
''; '';
} })
``` ```
### Custom build flags {#ssec-swiftpm-custom-build-flags} ### Custom build flags {#ssec-swiftpm-custom-build-flags}

View File

@ -94,18 +94,18 @@ Release 23.11 ships with a new interface that will eventually replace `texlive.c
- TeX Live packages are also available under `texlive.pkgs` as derivations with outputs `out`, `tex`, `texdoc`, `texsource`, `tlpkg`, `man`, `info`. They cannot be installed outside of `texlive.combine` but are available for other uses. To repackage a font, for instance, use - TeX Live packages are also available under `texlive.pkgs` as derivations with outputs `out`, `tex`, `texdoc`, `texsource`, `tlpkg`, `man`, `info`. They cannot be installed outside of `texlive.combine` but are available for other uses. To repackage a font, for instance, use
```nix ```nix
stdenvNoCC.mkDerivation rec { stdenvNoCC.mkDerivation (finalAttrs: {
src = texlive.pkgs.iwona; src = texlive.pkgs.iwona;
dontUnpack = true; dontUnpack = true;
inherit (src) pname version; inherit (finalAttrs.src) pname version;
installPhase = '' installPhase = ''
runHook preInstall runHook preInstall
install -Dm644 $src/fonts/opentype/nowacki/iwona/*.otf -t $out/share/fonts/opentype install -Dm644 $src/fonts/opentype/nowacki/iwona/*.otf -t $out/share/fonts/opentype
runHook postInstall runHook postInstall
''; '';
} })
``` ```
See `biber`, `iwona` for complete examples. See `biber`, `iwona` for complete examples.

View File

@ -20,14 +20,14 @@ stdenv.mkDerivation {
**Since [RFC 0035](https://github.com/NixOS/rfcs/pull/35), this is preferred for packages in Nixpkgs**, as it allows us to reuse the version easily: **Since [RFC 0035](https://github.com/NixOS/rfcs/pull/35), this is preferred for packages in Nixpkgs**, as it allows us to reuse the version easily:
```nix ```nix
stdenv.mkDerivation rec { stdenv.mkDerivation (finalAttrs: {
pname = "libfoo"; pname = "libfoo";
version = "1.2.3"; version = "1.2.3";
src = fetchurl { src = fetchurl {
url = "http://example.org/libfoo-source-${version}.tar.bz2"; url = "http://example.org/libfoo-source-${finalAttrs.version}.tar.bz2";
hash = "sha256-tWxU/LANbQE32my+9AXyt3nCT7NBVfJ45CX757EMT3Q="; hash = "sha256-tWxU/LANbQE32my+9AXyt3nCT7NBVfJ45CX757EMT3Q=";
}; };
} })
``` ```
Many packages have dependencies that are not provided in the standard environment. Its usually sufficient to specify those dependencies in the `buildInputs` attribute: Many packages have dependencies that are not provided in the standard environment. Its usually sufficient to specify those dependencies in the `buildInputs` attribute:
@ -63,6 +63,7 @@ stdenv.mkDerivation {
runHook postBuild runHook postBuild
''; '';
installPhase = '' installPhase = ''
runHook preInstall runHook preInstall
@ -222,12 +223,12 @@ These dependencies are only injected when [`doCheck`](#var-stdenv-doCheck) is se
Consider for example this simplified derivation for `solo5`, a sandboxing tool: Consider for example this simplified derivation for `solo5`, a sandboxing tool:
```nix ```nix
stdenv.mkDerivation rec { stdenv.mkDerivation (finalAttrs: {
pname = "solo5"; pname = "solo5";
version = "0.7.5"; version = "0.7.5";
src = fetchurl { src = fetchurl {
url = "https://github.com/Solo5/solo5/releases/download/v${version}/solo5-v${version}.tar.gz"; url = "https://github.com/Solo5/solo5/releases/download/v${finalAttrs.version}/solo5-v${finalAttrs.version}.tar.gz";
hash = "sha256-viwrS9lnaU8sTGuzK/+L/PlMM/xRRtgVuK5pixVeDEw="; hash = "sha256-viwrS9lnaU8sTGuzK/+L/PlMM/xRRtgVuK5pixVeDEw=";
}; };