File: components.nix

package info (click to toggle)
nix 2.26.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 87,540; sh: 8,864; perl: 649; yacc: 466; xml: 410; javascript: 378; lex: 329; ansic: 215; python: 128; sql: 56; makefile: 33; exp: 5; ruby: 1
file content (378 lines) | stat: -rw-r--r-- 12,010 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
{
  lib,
  pkgs,
  src,
  officialRelease,
}:

scope:

let
  inherit (scope)
    callPackage
    ;
  inherit
    (scope.callPackage (
      { stdenv }:
      {
        inherit stdenv;
      }
    ) { })
    stdenv
    ;
  inherit (pkgs.buildPackages)
    meson
    ninja
    pkg-config
    ;

  baseVersion = lib.fileContents ../.version;

  versionSuffix = lib.optionalString (!officialRelease) "pre";

  fineVersionSuffix =
    lib.optionalString (!officialRelease)
      "pre${
        builtins.substring 0 8 (src.lastModifiedDate or src.lastModified or "19700101")
      }_${src.shortRev or "dirty"}";

  fineVersion = baseVersion + fineVersionSuffix;

  root = ../.;

  # Indirection for Nixpkgs to override when package.nix files are vendored
  filesetToSource = lib.fileset.toSource;

  /**
    Given a set of layers, create a mkDerivation-like function
  */
  mkPackageBuilder =
    exts: userFn: stdenv.mkDerivation (lib.extends (lib.composeManyExtensions exts) userFn);

  setVersionLayer = finalAttrs: prevAttrs: {
    preConfigure =
      prevAttrs.preConfigure or ""
      +
        # Update the repo-global .version file.
        # Symlink ./.version points there, but by default only workDir is writable.
        ''
          chmod u+w ./.version
          echo ${finalAttrs.version} > ./.version
        '';
  };

  localSourceLayer =
    finalAttrs: prevAttrs:
    let
      workDirPath =
        # Ideally we'd pick finalAttrs.workDir, but for now `mkDerivation` has
        # the requirement that everything except passthru and meta must be
        # serialized by mkDerivation, which doesn't work for this.
        prevAttrs.workDir;

      workDirSubpath = lib.path.removePrefix root workDirPath;
      sources =
        assert prevAttrs.fileset._type == "fileset";
        prevAttrs.fileset;
      src = lib.fileset.toSource {
        fileset = sources;
        inherit root;
      };

    in
    {
      sourceRoot = "${src.name}/" + workDirSubpath;
      inherit src;

      # Clear what `derivation` can't/shouldn't serialize; see prevAttrs.workDir.
      fileset = null;
      workDir = null;
    };

  resolveRelPath = p: lib.path.removePrefix root p;

  makeFetchedSourceLayer =
    finalScope: finalAttrs: prevAttrs:
    let
      workDirPath =
        # Ideally we'd pick finalAttrs.workDir, but for now `mkDerivation` has
        # the requirement that everything except passthru and meta must be
        # serialized by mkDerivation, which doesn't work for this.
        prevAttrs.workDir;

      workDirSubpath = resolveRelPath workDirPath;

    in
    {
      sourceRoot = "${finalScope.patchedSrc.name}/" + workDirSubpath;
      src = finalScope.patchedSrc;
      version =
        let
          n = lib.length finalScope.patches;
        in
        if n == 0 then finalAttrs.version else finalAttrs.version + "+${toString n}";

      # Clear what `derivation` can't/shouldn't serialize; see prevAttrs.workDir.
      fileset = null;
      workDir = null;
    };

  mesonLayer = finalAttrs: prevAttrs: {
    # NOTE:
    # As of https://github.com/NixOS/nixpkgs/blob/8baf8241cea0c7b30e0b8ae73474cb3de83c1a30/pkgs/by-name/me/meson/setup-hook.sh#L26,
    # `mesonBuildType` defaults to `plain` if not specified. We want our Nix-built binaries to be optimized by default.
    # More on build types here: https://mesonbuild.com/Builtin-options.html#details-for-buildtype.
    mesonBuildType = "release";
    # NOTE:
    # Users who are debugging Nix builds are expected to set the environment variable `mesonBuildType`, per the
    # guidance in https://github.com/NixOS/nix/blob/8a3fc27f1b63a08ac983ee46435a56cf49ebaf4a/doc/manual/source/development/debugging.md?plain=1#L10.
    # For this reason, we don't want to refer to `finalAttrs.mesonBuildType` here, but rather use the environment variable.
    preConfigure =
      prevAttrs.preConfigure or ""
      +
        lib.optionalString
          (
            !stdenv.hostPlatform.isWindows
            # build failure
            && !stdenv.hostPlatform.isStatic
            # LTO breaks exception handling on x86-64-darwin.
            && stdenv.system != "x86_64-darwin"
          )
          ''
            case "$mesonBuildType" in
            release|minsize) appendToVar mesonFlags "-Db_lto=true"  ;;
            *)               appendToVar mesonFlags "-Db_lto=false" ;;
            esac
          '';
    nativeBuildInputs = [
      meson
      ninja
    ] ++ prevAttrs.nativeBuildInputs or [ ];
    mesonCheckFlags = prevAttrs.mesonCheckFlags or [ ] ++ [
      "--print-errorlogs"
    ];
  };

  mesonBuildLayer = finalAttrs: prevAttrs: {
    nativeBuildInputs = prevAttrs.nativeBuildInputs or [ ] ++ [
      pkg-config
    ];
    separateDebugInfo = !stdenv.hostPlatform.isStatic;
    hardeningDisable = lib.optional stdenv.hostPlatform.isStatic "pie";
    env =
      prevAttrs.env or { }
      // lib.optionalAttrs (
        stdenv.isLinux
        && !(stdenv.hostPlatform.isStatic && stdenv.system == "aarch64-linux")
        && !(stdenv.hostPlatform.useLLVM or false)
      ) { LDFLAGS = "-fuse-ld=gold"; };
  };

  mesonLibraryLayer = finalAttrs: prevAttrs: {
    outputs = prevAttrs.outputs or [ "out" ] ++ [ "dev" ];
  };

  # Work around weird `--as-needed` linker behavior with BSD, see
  # https://github.com/mesonbuild/meson/issues/3593
  bsdNoLinkAsNeeded =
    finalAttrs: prevAttrs:
    lib.optionalAttrs stdenv.hostPlatform.isBSD {
      mesonFlags = [ (lib.mesonBool "b_asneeded" false) ] ++ prevAttrs.mesonFlags or [ ];
    };

  miscGoodPractice = finalAttrs: prevAttrs: {
    strictDeps = prevAttrs.strictDeps or true;
    enableParallelBuilding = true;
  };

  /**
    Append patches to the source layer.
  */
  appendPatches =
    scope: patches:
    scope.overrideScope (
      finalScope: prevScope: {
        patches = prevScope.patches ++ patches;
      }
    );

in

# This becomes the pkgs.nixComponents attribute set
{
  version = baseVersion + versionSuffix;
  inherit versionSuffix;

  inherit filesetToSource;

  /**
    A user-provided extension function to apply to each component derivation.
  */
  mesonComponentOverrides = finalAttrs: prevAttrs: { };

  /**
    An overridable derivation layer for handling the sources.
  */
  sourceLayer = localSourceLayer;

  /**
    Resolve a path value to either itself or a path in the `src`, depending
    whether `overrideSource` was called.
  */
  resolvePath = p: p;

  /**
    Apply an extension function (i.e. overlay-shaped) to all component derivations.
  */
  overrideAllMesonComponents =
    f:
    scope.overrideScope (
      finalScope: prevScope: {
        mesonComponentOverrides = lib.composeExtensions scope.mesonComponentOverrides f;
      }
    );

  /**
    Provide an alternate source. This allows the expressions to be vendored without copying the sources,
    but it does make the build non-granular; all components will use a complete source.

    Packaging expressions will be ignored.
  */
  overrideSource =
    src:
    scope.overrideScope (
      finalScope: prevScope: {
        sourceLayer = makeFetchedSourceLayer finalScope;
        /**
          Unpatched source for the build of Nix. Packaging expressions will be ignored.
        */
        src = src;
        /**
          Patches for the whole Nix source. Changes to packaging expressions will be ignored.
        */
        patches = [ ];
        /**
          Fetched and patched source to be used in component derivations.
        */
        patchedSrc =
          if finalScope.patches == [ ] then
            src
          else
            pkgs.buildPackages.srcOnly (
              pkgs.buildPackages.stdenvNoCC.mkDerivation {
                name = "${finalScope.src.name or "nix-source"}-patched";
                inherit (finalScope) src patches;
              }
            );
        resolvePath = p: finalScope.patchedSrc + "/${resolveRelPath p}";
        appendPatches = appendPatches finalScope;
      }
    );

  /**
    Append patches to be applied to the whole Nix source.
    This affects all components.

    Changes to the packaging expressions will be ignored.
  */
  appendPatches =
    patches:
    # switch to "fetched" source first, so that patches apply to the whole tree.
    (scope.overrideSource "${./..}").appendPatches patches;

  mkMesonDerivation = mkPackageBuilder [
    miscGoodPractice
    scope.sourceLayer
    setVersionLayer
    mesonLayer
    scope.mesonComponentOverrides
  ];
  mkMesonExecutable = mkPackageBuilder [
    miscGoodPractice
    bsdNoLinkAsNeeded
    scope.sourceLayer
    setVersionLayer
    mesonLayer
    mesonBuildLayer
    scope.mesonComponentOverrides
  ];
  mkMesonLibrary = mkPackageBuilder [
    miscGoodPractice
    bsdNoLinkAsNeeded
    scope.sourceLayer
    mesonLayer
    setVersionLayer
    mesonBuildLayer
    mesonLibraryLayer
    scope.mesonComponentOverrides
  ];

  nix-util = callPackage ../src/libutil/package.nix { };
  nix-util-c = callPackage ../src/libutil-c/package.nix { };
  nix-util-test-support = callPackage ../src/libutil-test-support/package.nix { };
  nix-util-tests = callPackage ../src/libutil-tests/package.nix { };

  nix-store = callPackage ../src/libstore/package.nix { };
  nix-store-c = callPackage ../src/libstore-c/package.nix { };
  nix-store-test-support = callPackage ../src/libstore-test-support/package.nix { };
  nix-store-tests = callPackage ../src/libstore-tests/package.nix { };

  nix-fetchers = callPackage ../src/libfetchers/package.nix { };
  nix-fetchers-tests = callPackage ../src/libfetchers-tests/package.nix { };

  nix-expr = callPackage ../src/libexpr/package.nix { };
  nix-expr-c = callPackage ../src/libexpr-c/package.nix { };
  nix-expr-test-support = callPackage ../src/libexpr-test-support/package.nix { };
  nix-expr-tests = callPackage ../src/libexpr-tests/package.nix { };

  nix-flake = callPackage ../src/libflake/package.nix { };
  nix-flake-c = callPackage ../src/libflake-c/package.nix { };
  nix-flake-tests = callPackage ../src/libflake-tests/package.nix { };

  nix-main = callPackage ../src/libmain/package.nix { };
  nix-main-c = callPackage ../src/libmain-c/package.nix { };

  nix-cmd = callPackage ../src/libcmd/package.nix { };

  nix-cli = callPackage ../src/nix/package.nix { version = fineVersion; };

  nix-functional-tests = callPackage ../tests/functional/package.nix {
    version = fineVersion;
  };

  nix-manual = callPackage ../doc/manual/package.nix { version = fineVersion; };
  nix-internal-api-docs = callPackage ../src/internal-api-docs/package.nix { version = fineVersion; };
  nix-external-api-docs = callPackage ../src/external-api-docs/package.nix { version = fineVersion; };

  nix-perl-bindings = callPackage ../src/perl/package.nix { };

  nix-everything = callPackage ../packaging/everything.nix { } // {
    # Note: no `passthru.overrideAllMesonComponents`
    #       This would propagate into `nix.overrideAttrs f`, but then discard
    #       `f` when `.overrideAllMesonComponents` is used.
    #       Both "methods" should be views on the same fixpoint overriding mechanism
    #       for that to work. For now, we intentionally don't support the broken
    #       two-fixpoint solution.
    /**
      Apply an extension function (i.e. overlay-shaped) to all component derivations, and return the nix package.
    */
    overrideAllMesonComponents = f: (scope.overrideAllMesonComponents f).nix-everything;

    /**
      Append patches to be applied to the whole Nix source.
      This affects all components.

      Changes to the packaging expressions will be ignored.
    */
    appendPatches = ps: (scope.appendPatches ps).nix-everything;

    /**
      Provide an alternate source. This allows the expressions to be vendored without copying the sources,
      but it does make the build non-granular; all components will use a complete source.

      Packaging expressions will be ignored.
    */
    overrideSource = src: (scope.overrideSource src).nix-everything;

  };
}