{
  description = "Home Manager for Nix";

  inputs.nixpkgs.url = "https://channels.nixos.org/nixpkgs-unstable/nixexprs.tar.zst";

  outputs =
    {
      self,
      nixpkgs,
      ...
    }:
    {
      nixosModules = rec {
        home-manager = ./nixos;
        default = home-manager;
      };

      darwinModules = rec {
        home-manager = ./nix-darwin;
        default = home-manager;
      };

      flakeModules = rec {
        home-manager = ./flake-module.nix;
        default = home-manager;
      };

      templates = {
        default = self.templates.standalone;
        nixos = {
          path = ./templates/nixos;
          description = "Home Manager as a NixOS module,";
        };
        nix-darwin = {
          path = ./templates/nix-darwin;
          description = "Home Manager as a nix-darwin module,";
        };
        standalone = {
          path = ./templates/standalone;
          description = "Standalone setup";
        };
      };

      lib = import ./lib { inherit (nixpkgs) lib; };
    }
    // (
      let
        supportedSystems = [
          "aarch64-darwin"
          "aarch64-linux"
          "i686-linux"
          "x86_64-linux"
        ];

        forSystems = systems: f: nixpkgs.lib.genAttrs systems (system: f nixpkgs.legacyPackages.${system});

        forAllPkgs = forSystems (nixpkgs.lib.remove "x86_64-darwin" nixpkgs.lib.systems.flakeExposed);

        forSupportedPkgs = forSystems supportedSystems;

        forCI = nixpkgs.lib.genAttrs [
          "aarch64-darwin"
          "x86_64-linux"
        ];

        warn = builtins.warn or nixpkgs.lib.warn;

        releaseInfo = nixpkgs.lib.importJSON ./release.json;

        docsFor =
          pkgs:
          import ./docs {
            inherit pkgs;
            inherit (releaseInfo) release isReleaseBranch;
          };

        testChunks =
          system:
          let
            pkgs = nixpkgs.legacyPackages.${system};
            inherit (pkgs) lib;

            # Create chunked test packages for better CI parallelization
            tests = import ./tests {
              inherit pkgs;
              enableBig = true;
            };
            allTests = lib.attrNames tests.build;
            # Remove 'all' from the test list as it's a meta-package
            filteredTests = lib.filter (name: name != "all") allTests;
            # NOTE: Just a starting value, we can tweak this to find a good value.
            targetTestsPerChunk = 50;
            numChunks = lib.max 1 (
              builtins.ceil ((builtins.length filteredTests) / (targetTestsPerChunk * 1.0))
            );
            chunkSize = builtins.ceil ((builtins.length filteredTests) / (numChunks * 1.0));

            makeChunk =
              chunkNum: testList:
              let
                start = (chunkNum - 1) * chunkSize;
                end = lib.min (start + chunkSize) (builtins.length testList);
                chunkTests = lib.sublist start (end - start) testList;
                chunkAttrs = lib.genAttrs chunkTests (name: tests.build.${name});
              in
              pkgs.symlinkJoin {
                name = "test-chunk-${toString chunkNum}";
                paths = lib.attrValues chunkAttrs;
                passthru.tests = chunkTests;
              };
          in
          lib.listToAttrs (
            lib.genList (
              i: lib.nameValuePair "test-chunk-${toString (i + 1)}" (makeChunk (i + 1) filteredTests)
            ) numChunks
          );

        integrationTests =
          system:
          let
            pkgs = nixpkgs.legacyPackages.${system};
            inherit (pkgs) lib;
          in
          lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux (
            let
              tests = import ./tests/integration { inherit pkgs lib; };
              renameTestPkg = n: v: lib.nameValuePair "integration-${n}" v;
            in
            lib.mapAttrs' renameTestPkg (lib.removeAttrs tests [ "all" ])
          );

        buildTests =
          system:
          let
            pkgs = nixpkgs.legacyPackages.${system};
            tests = import ./tests { inherit pkgs; };
            renameTestPkg = n: nixpkgs.lib.nameValuePair "test-${n}";
          in
          nixpkgs.lib.mapAttrs' renameTestPkg tests.build;

        buildTestsNoBig =
          system:
          let
            pkgs = nixpkgs.legacyPackages.${system};
            tests = import ./tests {
              inherit pkgs;
              enableBig = false;
            };
          in
          {
            test-all-enableBig-false-enableLegacyIfd-false = tests.build.all;
          };

        buildTestsNoBigIfd =
          system:
          let
            pkgs = nixpkgs.legacyPackages.${system};
            tests = import ./tests {
              inherit pkgs;
              enableBig = false;
              enableLegacyIfd = true;
            };
          in
          {
            test-all-enableBig-false-enableLegacyIfd-true = tests.build.all;
          };

        integrationTestPackages =
          system:
          let
            pkgs = nixpkgs.legacyPackages.${system};
            inherit (pkgs) lib;
            tests = import ./tests/integration { inherit pkgs lib; };
            renameTestPkg = n: lib.nameValuePair "integration-test-${n}";
          in
          lib.mapAttrs' renameTestPkg tests;
      in
      {
        formatter = forSupportedPkgs (pkgs: pkgs.callPackage ./home-manager/formatter.nix { });

        # TODO: increase nixbot testing scope
        nixbot = forCI (
          system:
          let
            docs = docsFor nixpkgs.legacyPackages.${system};
            allIntegrationTests = integrationTests system;
            workingIntegrationTests = nixpkgs.lib.filterAttrs (
              name: _:
              nixpkgs.lib.elem name [
                "integration-nixos-basics"
                "integration-nixos-legacy-profile-management"
              ]
            ) allIntegrationTests;
          in
          (testChunks system)
          // workingIntegrationTests
          // {
            docs-html = docs.manual.html;
            docs-json = docs.options.json;
            docs-jsonModuleMaintainers = docs.jsonModuleMaintainers;
            docs-manpages = docs.manPages;
          }
        );

        # Alias for nixbot, added 2026-07-30
        buildbot = forCI (
          system:
          warn "Home Manager: `buildbot.${system}` has been renamed to `nixbot.${system}`."
            self.nixbot.${system}
        );

        packages = forAllPkgs (
          pkgs:
          let
            docs = docsFor pkgs;
            hmPkg = pkgs.callPackage ./home-manager { path = "${self}"; };
          in
          {
            default = hmPkg;
            home-manager = hmPkg;
          }
          // nixpkgs.lib.optionalAttrs (nixpkgs.lib.elem pkgs.stdenv.hostPlatform.system supportedSystems) {

            ci-parse = pkgs.callPackage ./ci/parse.nix { nix = pkgs.nixVersions.latest; };
            ci-parse-lix = pkgs.callPackage ./ci/parse.nix {
              nix = pkgs.lixPackageSets.latest.lix;
            };

            create-news-entry = pkgs.writeShellScriptBin "create-news-entry" ''
              ./modules/misc/news/create-news-entry.sh
            '';

            tests = pkgs.callPackage ./tests/package.nix {
              flake = self;
              inputOverrides = {
                inherit nixpkgs;
              };
            };

            docs-html = docs.manual.html;
            docs-htmlOpenTool = docs.manual.htmlOpenTool;
            docs-json = docs.options.json;
            docs-jsonModuleMaintainers = docs.jsonModuleMaintainers;
            docs-manpages = docs.manPages;
          }
        );

        devShells = forSupportedPkgs (pkgs: {
          default = pkgs.callPackage ./home-manager/devShell.nix { };
        });

        legacyPackages = forSupportedPkgs (
          pkgs:
          let
            inherit (pkgs.stdenv.hostPlatform) system;
          in
          (buildTests system)
          // (integrationTestPackages system)
          // (buildTestsNoBig system)
          // (buildTestsNoBigIfd system)
          // (testChunks system)
          // (integrationTests system)
        );
      }
    );
}