lol

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.

authored by

Sophie Tauchert and committed by
Emery Hemingway
3b40e1bd db3bd72e

+36 -2
+15 -2
doc/languages-frameworks/nim.section.md
··· 50 50 $ nix run -f . nim_lk ./result | jq --sort-keys > pkgs/by-name/tt/ttop/lock.json 51 51 ``` 52 52 53 - ## Lockfile dependency overrides {#nimoverrides} 53 + ## Overriding Nim packages {#nim-overrides} 54 + 55 + 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`: 56 + 57 + ```nix 58 + pkgs.nitter.overrideNimAttrs { 59 + # using a different source which has different dependencies from the standard package 60 + src = pkgs.fetchFromGithub { /* … */ }; 61 + # new lock file generated from the source 62 + lockFile = ./custom-lock.json; 63 + } 64 + ``` 65 + 66 + ## Lockfile dependency overrides {#nim-lock-overrides} 54 67 55 68 The `buildNimPackage` function matches the libraries specified by `lockFile` to attrset of override functions that are then applied to the package derivation. 56 69 The default overrides are maintained as the top-level `nimOverrides` attrset at `pkgs/top-level/nim-overrides.nix`. ··· 81 94 - finalAttrs: the final attrset passed by `buildNimPackage` to `stdenv.mkDerivation`. 82 95 - prevAttrs: the attrset produced by initial arguments to `buildNimPackage` and any preceding lockfile overlays. 83 96 84 - ### Overriding an Nim library override {#nimoverrides-overrides} 97 + ### Overriding an Nim library override {#nim-lock-overrides-overrides} 85 98 86 99 The `nimOverrides` attrset makes it possible to modify overrides in a few different ways. 87 100
+21
pkgs/development/compilers/nim/build-nim-package.nix
··· 7 7 , nim_builder 8 8 , defaultNimVersion ? 2 9 9 , nimOverrides 10 + , buildNimPackage 10 11 }: 11 12 12 13 let ··· 90 91 , nativeBuildInputs ? [ ] 91 92 , nimFlags ? [ ] 92 93 , requiredNimVersion ? defaultNimVersion 94 + , passthru ? { } 93 95 , ... 94 96 }: 95 97 (if requiredNimVersion == 1 then { ··· 102 104 throw 103 105 "requiredNimVersion ${toString requiredNimVersion} is not valid") // { 104 106 nimFlags = lockFileNimFlags ++ nimFlags; 107 + passthru = passthru // { 108 + # allow overriding the result of buildNimPackageArgs before this composition is applied 109 + # this allows overriding the lockFile for packages built using buildNimPackage 110 + # this is adapted from mkDerivationExtensible in stdenv.mkDerivation 111 + overrideNimAttrs = f0: 112 + let 113 + f = self: super: 114 + let x = f0 super; 115 + in 116 + if builtins.isFunction x 117 + then f0 self super 118 + else x; 119 + in 120 + buildNimPackage 121 + (self: 122 + let super = (asFunc ((asFunc buildNimPackageArgs) self)) baseAttrs; 123 + in 124 + super // (if builtins.isFunction f0 || f0?__functor then f self super else f0)); 125 + }; 105 126 }; 106 127 107 128 attrs = postLock // finalOverride postLock;