ch9344: 2.0 -> 0-unstable-2024-11-15 (#354536)
[NixPkgs.git] / pkgs / build-support / build-nim-package.nix
blob1e8489212cd6bfe620328277f29efba0ca575e52
2   lib,
3   buildPackages,
4   callPackage,
5   stdenv,
6   nim1,
7   nim2,
8   nim_builder,
9   defaultNimVersion ? 2,
10   nimOverrides,
11   buildNimPackage,
14 let
15   baseAttrs = {
16     strictDeps = true;
17     enableParallelBuilding = true;
18     doCheck = true;
19     configurePhase = ''
20       runHook preConfigure
21       export NIX_NIM_BUILD_INPUTS=''${pkgsHostTarget[@]} $NIX_NIM_BUILD_INPUTS
22       nim_builder --phase:configure
23       runHook postConfigure
24     '';
25     buildPhase = ''
26       runHook preBuild
27       nim_builder --phase:build
28       runHook postBuild
29     '';
30     checkPhase = ''
31       runHook preCheck
32       nim_builder --phase:check
33       runHook postCheck
34     '';
35     installPhase = ''
36       runHook preInstall
37       nim_builder --phase:install
38       runHook postInstall
39     '';
40     meta = { inherit (nim2.meta) maintainers platforms; };
41   };
43   fodFromLockEntry =
44     let
45       methods = {
46         fetchzip =
47           { url, sha256, ... }:
48           buildPackages.fetchzip {
49             name = "source";
50             inherit url sha256;
51           };
52         git =
53           {
54             fetchSubmodules,
55             leaveDotGit,
56             rev,
57             sha256,
58             url,
59             ...
60           }:
61           buildPackages.fetchgit {
62             inherit
63               fetchSubmodules
64               leaveDotGit
65               rev
66               sha256
67               url
68               ;
69           };
70       };
71     in
72     attrs@{ method, ... }:
73     let
74       fod = methods.${method} attrs;
75     in
76     ''--path:"${fod.outPath}/${attrs.srcDir}"'';
78   asFunc = x: if builtins.isFunction x then x else (_: x);
81 buildNimPackageArgs:
82 let
83   composition =
84     finalAttrs:
85     let
86       postPkg = baseAttrs // (asFunc ((asFunc buildNimPackageArgs) finalAttrs)) baseAttrs;
88       lockAttrs = lib.attrsets.optionalAttrs (builtins.hasAttr "lockFile" postPkg) (
89         builtins.fromJSON (builtins.readFile postPkg.lockFile)
90       );
92       lockDepends = lockAttrs.depends or [ ];
94       lockFileNimFlags = map fodFromLockEntry lockDepends;
96       postNimOverrides = builtins.foldl' (
97         prevAttrs:
98         { packages, ... }@lockAttrs:
99         builtins.foldl' (
100           prevAttrs: name:
101           if (builtins.hasAttr name nimOverrides) then
102             (prevAttrs // (nimOverrides.${name} lockAttrs prevAttrs))
103           else
104             prevAttrs
105         ) prevAttrs packages
106       ) postPkg lockDepends;
108       finalOverride =
109         {
110           depsBuildBuild ? [ ],
111           nativeBuildInputs ? [ ],
112           nimFlags ? [ ],
113           requiredNimVersion ? defaultNimVersion,
114           passthru ? { },
115           ...
116         }:
117         (
118           if requiredNimVersion == 1 then
119             {
120               depsBuildBuild = [ nim_builder ] ++ depsBuildBuild;
121               nativeBuildInputs = [ nim1 ] ++ nativeBuildInputs;
122             }
123           else if requiredNimVersion == 2 then
124             {
125               depsBuildBuild = [ nim_builder ] ++ depsBuildBuild;
126               nativeBuildInputs = [ nim2 ] ++ nativeBuildInputs;
127             }
128           else
129             throw "requiredNimVersion ${toString requiredNimVersion} is not valid"
130         )
131         // {
132           nimFlags = lockFileNimFlags ++ nimFlags;
133           passthru = passthru // {
134             # allow overriding the result of buildNimPackageArgs before this composition is applied
135             # this allows overriding the lockFile for packages built using buildNimPackage
136             # this is adapted from mkDerivationExtensible in stdenv.mkDerivation
137             overrideNimAttrs =
138               f0:
139               let
140                 f =
141                   self: super:
142                   let
143                     x = f0 super;
144                   in
145                   if builtins.isFunction x then f0 self super else x;
146               in
147               buildNimPackage (
148                 self:
149                 let
150                   super = (asFunc ((asFunc buildNimPackageArgs) self)) baseAttrs;
151                 in
152                 super // (if builtins.isFunction f0 || f0 ? __functor then f self super else f0)
153               );
154           };
155         };
157       attrs = postNimOverrides // finalOverride postNimOverrides;
158     in
159     lib.trivial.warnIf (builtins.hasAttr "nimBinOnly" attrs)
160       "the nimBinOnly attribute is deprecated for buildNimPackage"
161       attrs;
164 stdenv.mkDerivation composition