From 3b40e1bd33b442bb705f88a2bada2f6ace51e607 Mon Sep 17 00:00:00 2001 From: Sophie Tauchert Date: Wed, 6 Dec 2023 09:44:17 +0100 Subject: buildNimPackage: allow overriding nim package args Without this, it's impossible to override the lockFile as the default overrideAttrs is applied after the composition in buildNimPackage has read the lock file and generated the nim flags from it. --- doc/languages-frameworks/nim.section.md | 17 +++++++++++++++-- .../development/compilers/nim/build-nim-package.nix | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/doc/languages-frameworks/nim.section.md b/doc/languages-frameworks/nim.section.md index 45cd07b3a3d8..c6ebf49b83f6 100644 --- a/doc/languages-frameworks/nim.section.md +++ b/doc/languages-frameworks/nim.section.md @@ -50,7 +50,20 @@ $ nix build -f . ttop.src $ nix run -f . nim_lk ./result | jq --sort-keys > pkgs/by-name/tt/ttop/lock.json ``` -## Lockfile dependency overrides {#nimoverrides} +## Overriding Nim packages {#nim-overrides} + +The `buildNimPackage` function generates flags and additional build dependencies from the `lockFile` parameter passed to `buildNimPackage`. Using [`overrideAttrs`](#sec-pkg-overrideAttrs) on the final package will apply after this has already been generated, so this can't be used to override the `lockFile` in a package built with `buildNimPackage`. To be able to override parameters before flags and build dependencies are generated from the `lockFile`, use `overrideNimAttrs` instead with the same syntax as `overrideAttrs`: + +```nix +pkgs.nitter.overrideNimAttrs { + # using a different source which has different dependencies from the standard package + src = pkgs.fetchFromGithub { /* … */ }; + # new lock file generated from the source + lockFile = ./custom-lock.json; +} +``` + +## Lockfile dependency overrides {#nim-lock-overrides} The `buildNimPackage` function matches the libraries specified by `lockFile` to attrset of override functions that are then applied to the package derivation. The default overrides are maintained as the top-level `nimOverrides` attrset at `pkgs/top-level/nim-overrides.nix`. @@ -81,7 +94,7 @@ The annotations in the `nim-overrides.nix` set are functions that take three arg - finalAttrs: the final attrset passed by `buildNimPackage` to `stdenv.mkDerivation`. - prevAttrs: the attrset produced by initial arguments to `buildNimPackage` and any preceding lockfile overlays. -### Overriding an Nim library override {#nimoverrides-overrides} +### Overriding an Nim library override {#nim-lock-overrides-overrides} The `nimOverrides` attrset makes it possible to modify overrides in a few different ways. diff --git a/pkgs/development/compilers/nim/build-nim-package.nix b/pkgs/development/compilers/nim/build-nim-package.nix index 5085edf90a76..a22fb45b507b 100644 --- a/pkgs/development/compilers/nim/build-nim-package.nix +++ b/pkgs/development/compilers/nim/build-nim-package.nix @@ -7,6 +7,7 @@ , nim_builder , defaultNimVersion ? 2 , nimOverrides +, buildNimPackage }: let @@ -90,6 +91,7 @@ let , nativeBuildInputs ? [ ] , nimFlags ? [ ] , requiredNimVersion ? defaultNimVersion + , passthru ? { } , ... }: (if requiredNimVersion == 1 then { @@ -102,6 +104,25 @@ let throw "requiredNimVersion ${toString requiredNimVersion} is not valid") // { nimFlags = lockFileNimFlags ++ nimFlags; + passthru = passthru // { + # allow overriding the result of buildNimPackageArgs before this composition is applied + # this allows overriding the lockFile for packages built using buildNimPackage + # this is adapted from mkDerivationExtensible in stdenv.mkDerivation + overrideNimAttrs = f0: + let + f = self: super: + let x = f0 super; + in + if builtins.isFunction x + then f0 self super + else x; + in + buildNimPackage + (self: + let super = (asFunc ((asFunc buildNimPackageArgs) self)) baseAttrs; + in + super // (if builtins.isFunction f0 || f0?__functor then f self super else f0)); + }; }; attrs = postLock // finalOverride postLock; -- cgit 1.4.1