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.
This commit is contained in:
Sophie Tauchert 2023-12-06 09:44:17 +01:00 committed by Emery Hemingway
parent db3bd72ed2
commit 3b40e1bd33
2 changed files with 36 additions and 2 deletions

View File

@ -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.

View File

@ -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;