all.nixnix · 120 linesall
{ lib, ... }:
{
  perSystem =
    {
      config,
      pkgs,
      ...
    }:
    let
      # The Android cross-compilation outputs (nix/packages/android/) live in
      # their own aggregate (`all-android`, built by the `nix-build-android`
      # CI job): they multiply the closure by gigabytes (dual-ABI druntimes,
      # the NDK/SDK) and pull the unfree Android SDK — neither belongs in the
      # desktop cache aggregate.
      #
      # The android module DECLARES the names it owns rather than this file
      # matching on the string "android". A heuristic leaked in both
      # directions: an Android output not containing the word would join the
      # desktop aggregate and get built on macOS, and a desktop package that
      # did contain it would be silently dropped from CI. (The font build was
      # the live example — it lived under nix/packages/android/ but is a plain
      # cross-platform font package, and is now a first-class desktop output
      # in nix/packages/fonts.nix.)
      #
      # `or [ ]` because the android module only defines its outputs on
      # x86_64-linux; everywhere else there is nothing to subtract.
      exclusions = import ../flake-exclusions.nix;
      androidNames = config.legacyPackages.androidPackageNames or [ ];

      # Every desktop derivation the `nix-build` CI job builds and pushes to
      # the binary cache: the full dev shell, every (non-Android) package,
      # and every standalone example (examples.<lib>.<name>, flattened). New
      # outputs are picked up automatically.
      desktopEntries = {
        devshell-full = config.devShells.full;
        # The lean shell the CI jobs actually activate. It must be in the
        # aggregate or it never reaches the binary cache, and every runner
        # rebuilds it from source.
        devshell-ci = config.devShells.ci;
        # The lint job's shell (prek and the hooks' tools).
        devshell-pre-commit = config.devShells.pre-commit;
      }
      // builtins.removeAttrs config.packages (
        [ "all-desktop" ] ++ androidNames ++ exclusions.excludedCiPackages
      )
      // lib.concatMapAttrs (
        libName: lib.mapAttrs' (exName: lib.nameValuePair "example-${libName}-${exName}")
      ) config.legacyPackages.examples;

      # The shards CI's x86_64 Linux leg builds the aggregate in, each on a
      # runner of its own and in parallel (.github/workflows/ci.yml,
      # `nix-build`). That leg builds one derivation at a time (more ran a
      # runner out of memory), and a change to a library every application
      # links rebuilds about 25 of them, each compiling its own dependencies
      # for 2–6 minutes: 90 minutes in one job in run 46293237357, the job's
      # cap. The windowed applications are split between `gui-1` and `gui-2`,
      # balanced by that run's times. `examples` holds every example and what
      # the job checks after the build: `run-all-examples`, which references
      # every example, and the shaders and WebAssembly modules it hashes.
      # `tools` takes everything else, `ci` and the dev shells among it, so an
      # output nobody assigned is still built.
      shardMembers = {
        gui-1 = [
          "hue"
          "terminal"
          "text-lab"
          "fileset-explorer"
          "ui-vulkan-shaders"
        ];
        gui-2 = [
          "hue-raylib"
          "terminal-raylib"
          "ui-gallery"
          "ui-gallery-raylib"
          "editor"
          "media-hw-probe"
        ];
        examples = [
          "run-all-examples"
          "ui-shaders"
          "vulkan-wsi-shaders"
          "text-wasm"
          "table-wasm"
          "wired-wasm"
          "dub-manifest-nix-wasm"
        ];
      };
      exampleEntries = lib.concatMapAttrs (
        libName: lib.mapAttrs' (exName: _: lib.nameValuePair "example-${libName}-${exName}" true)
      ) config.legacyPackages.examples;
      shardOf =
        name:
        let
          claims = builtins.attrNames (lib.filterAttrs (_: builtins.elem name) shardMembers);
        in
        if exampleEntries ? ${name} then
          "examples"
        else if claims == [ ] then
          "tools"
        else
          lib.head claims;
    in
    {
      packages.all-desktop = pkgs.linkFarm "sparkles-all-desktop" desktopEntries;

      # `allDesktopShards.<shard>`: the aggregate's entries partitioned among
      # the shards above; together they are exactly `all-desktop`'s.
      # Every shard exists, empty or not: CI names each.
      legacyPackages.allDesktopShards =
        let
          grouped = lib.groupBy shardOf (builtins.attrNames desktopEntries);
        in
        lib.genAttrs ([ "tools" ] ++ builtins.attrNames shardMembers) (
          shard:
          pkgs.linkFarm "sparkles-all-desktop-${shard}" (
            lib.getAttrs (grouped.${shard} or [ ]) desktopEntries
          )
        );
    };
}