about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/cuda-modules/generic-builders/multiplex.nix
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2023-12-15 19:32:38 +0100
committerAlyssa Ross <hi@alyssa.is>2023-12-15 19:32:38 +0100
commit6b8e2555ef013b579cda57025b17d662e0f1fe1f (patch)
tree5a83c673af26c9976acd5a5dfa20e09e06898047 /nixpkgs/pkgs/development/cuda-modules/generic-builders/multiplex.nix
parent66ca7a150b5c051f0728f13134e6265cc46f370c (diff)
parent02357adddd0889782362d999628de9d309d202dc (diff)
downloadnixlib-6b8e2555ef013b579cda57025b17d662e0f1fe1f.tar
nixlib-6b8e2555ef013b579cda57025b17d662e0f1fe1f.tar.gz
nixlib-6b8e2555ef013b579cda57025b17d662e0f1fe1f.tar.bz2
nixlib-6b8e2555ef013b579cda57025b17d662e0f1fe1f.tar.lz
nixlib-6b8e2555ef013b579cda57025b17d662e0f1fe1f.tar.xz
nixlib-6b8e2555ef013b579cda57025b17d662e0f1fe1f.tar.zst
nixlib-6b8e2555ef013b579cda57025b17d662e0f1fe1f.zip
Merge branch 'nixos-unstable-small' of https://github.com/NixOS/nixpkgs
Diffstat (limited to 'nixpkgs/pkgs/development/cuda-modules/generic-builders/multiplex.nix')
-rw-r--r--nixpkgs/pkgs/development/cuda-modules/generic-builders/multiplex.nix131
1 files changed, 131 insertions, 0 deletions
diff --git a/nixpkgs/pkgs/development/cuda-modules/generic-builders/multiplex.nix b/nixpkgs/pkgs/development/cuda-modules/generic-builders/multiplex.nix
new file mode 100644
index 000000000000..b8053094bcc8
--- /dev/null
+++ b/nixpkgs/pkgs/development/cuda-modules/generic-builders/multiplex.nix
@@ -0,0 +1,131 @@
+{
+  # callPackage-provided arguments
+  lib,
+  cudaVersion,
+  flags,
+  hostPlatform,
+  # Expected to be passed by the caller
+  mkVersionedPackageName,
+  # pname :: String
+  pname,
+  # releasesModule :: Path
+  # A path to a module which provides a `releases` attribute
+  releasesModule,
+  # shims :: Path
+  # A path to a module which provides a `shims` attribute
+  # The redistribRelease is only used in ./manifest.nix for the package version
+  # and the package description (which NVIDIA's manifest calls the "name").
+  # It's also used for fetching the source, but we override that since we can't
+  # re-use that portion of the functionality (different URLs, etc.).
+  # The featureRelease is used to populate meta.platforms (by way of looking at the attribute names)
+  # and to determine the outputs of the package.
+  # shimFn :: {package, redistArch} -> AttrSet
+  shimsFn ? ({package, redistArch}: throw "shimsFn must be provided"),
+  # fixupFn :: Path
+  # A path (or nix expression) to be evaluated with callPackage and then
+  # provided to the package's overrideAttrs function.
+  # It must accept at least the following arguments:
+  # - final
+  # - cudaVersion
+  # - mkVersionedPackageName
+  # - package
+  fixupFn ? (
+    {
+      final,
+      cudaVersion,
+      mkVersionedPackageName,
+      package,
+      ...
+    }:
+    throw "fixupFn must be provided"
+  ),
+}:
+let
+  inherit (lib)
+    attrsets
+    lists
+    modules
+    strings
+    ;
+
+  evaluatedModules = modules.evalModules {
+    modules = [
+      ../modules
+      releasesModule
+    ];
+  };
+
+  # NOTE: Important types:
+  # - Releases: ../modules/${pname}/releases/releases.nix
+  # - Package: ../modules/${pname}/releases/package.nix
+
+  # All releases across all platforms
+  # See ../modules/${pname}/releases/releases.nix
+  allReleases = evaluatedModules.config.${pname}.releases;
+
+  # Compute versioned attribute name to be used in this package set
+  # Patch version changes should not break the build, so we only use major and minor
+  # computeName :: Package -> String
+  computeName = {version, ...}: mkVersionedPackageName pname version;
+
+  # Check whether a package supports our CUDA version
+  # isSupported :: Package -> Bool
+  isSupported =
+    package:
+    strings.versionAtLeast cudaVersion package.minCudaVersion
+    && strings.versionAtLeast package.maxCudaVersion cudaVersion;
+
+  # Get all of the packages for our given platform.
+  redistArch = flags.getRedistArch hostPlatform.system;
+
+  # All the supported packages we can build for our platform.
+  # supportedPackages :: List (AttrSet Packages)
+  supportedPackages = builtins.filter isSupported (allReleases.${redistArch} or []);
+
+  # newestToOldestSupportedPackage :: List (AttrSet Packages)
+  newestToOldestSupportedPackage = lists.reverseList supportedPackages;
+
+  nameOfNewest = computeName (builtins.head newestToOldestSupportedPackage);
+
+  # A function which takes the `final` overlay and the `package` being built and returns
+  # a function to be consumed via `overrideAttrs`.
+  overrideAttrsFixupFn =
+    final: package:
+    final.callPackage fixupFn {
+      inherit
+        final
+        cudaVersion
+        mkVersionedPackageName
+        package
+        ;
+    };
+
+  extension =
+    final: _:
+    let
+      # Builds our package into derivation and wraps it in a nameValuePair, where the name is the versioned name
+      # of the package.
+      buildPackage =
+        package:
+        let
+          shims = final.callPackage shimsFn {inherit package redistArch;};
+          name = computeName package;
+          drv = final.callPackage ./manifest.nix {
+            inherit pname;
+            redistName = pname;
+            inherit (shims) redistribRelease featureRelease;
+          };
+          fixedDrv = drv.overrideAttrs (overrideAttrsFixupFn final package);
+        in
+        attrsets.nameValuePair name fixedDrv;
+
+      # versionedDerivations :: AttrSet Derivation
+      versionedDerivations = builtins.listToAttrs (lists.map buildPackage newestToOldestSupportedPackage);
+
+      defaultDerivation = attrsets.optionalAttrs (versionedDerivations != {}) {
+        ${pname} = versionedDerivations.${nameOfNewest};
+      };
+    in
+    versionedDerivations // defaultDerivation;
+in
+extension