From 4a644607c62d639fa39361522990525b9274f0a7 Mon Sep 17 00:00:00 2001 From: Jan-Bulthuis Date: Wed, 28 May 2025 13:39:00 +0200 Subject: [PATCH] Restructured glue --- flake.lock | 12 --- flake.nix | 3 +- glue/default.nix | 167 +++++++++++++++++++++++++++++++++++++++++ glue/flake.nix | 14 ---- glue/lib/default.nix | 173 ------------------------------------------- 5 files changed, 168 insertions(+), 201 deletions(-) create mode 100644 glue/default.nix delete mode 100644 glue/flake.nix delete mode 100644 glue/lib/default.nix diff --git a/flake.lock b/flake.lock index c342218..cd645d1 100644 --- a/flake.lock +++ b/flake.lock @@ -34,17 +34,6 @@ "type": "github" } }, - "glue": { - "locked": { - "path": "./glue", - "type": "path" - }, - "original": { - "path": "./glue", - "type": "path" - }, - "parent": [] - }, "home-manager": { "inputs": { "nixpkgs": [ @@ -125,7 +114,6 @@ }, "root": { "inputs": { - "glue": "glue", "home-manager": "home-manager", "nix-minecraft": "nix-minecraft", "nix-modpack": "nix-modpack", diff --git a/flake.nix b/flake.nix index e6871b3..131919f 100644 --- a/flake.nix +++ b/flake.nix @@ -2,7 +2,6 @@ description = "System configuration for NixOS"; inputs = { - glue.url = "./glue"; nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; home-manager.url = "github:nix-community/home-manager"; home-manager.inputs.nixpkgs.follows = "nixpkgs"; @@ -12,5 +11,5 @@ nix-modpack.inputs.nixpkgs.follows = "nixpkgs"; }; - outputs = inputs: inputs.glue inputs; + outputs = inputs: import ./glue inputs; } diff --git a/glue/default.nix b/glue/default.nix new file mode 100644 index 0000000..073e700 --- /dev/null +++ b/glue/default.nix @@ -0,0 +1,167 @@ +inputs: +let + flake = inputs.self; + nixpkgs = inputs.nixpkgs; + lib = nixpkgs.lib; + + importDir = + path: fn: + let + entries = builtins.readDir path; + + # Get paths to directories + dirs = lib.filterAttrs (_: type: type == "directory") entries; + dirPaths = lib.mapAttrs (name: type: { + path = "${path}/${name}"; + type = type; + }) dirs; + + # Get paths to nix files + nixName = name: builtins.match "(.*)\\.nix" name; + files = lib.filterAttrs (name: type: (type != "directory") && ((nixName name) != null)) entries; + filePaths = lib.mapAttrs' (name: type: { + name = builtins.head (nixName name); + value = { + path = "${path}/${name}"; + type = type; + }; + }) files; + + combined = dirPaths // filePaths; + in + fn (lib.optionalAttrs (builtins.pathExists path) combined); + + # Split out into getNixFiles, getNixFilesRecursive, getDirs + importDirRecursive = + path: fn: + let + entries = importDir path lib.id; + + # Dig down recursively + dirs = lib.filterAttrs (_: entry: entry.type == "directory") entries; + recursedEntries = lib.mapAttrs (name: entry: (importDirRecursive entry.path lib.id)) dirs; + in + fn (entries // recursedEntries); + + eachSystem = fn: lib.genAttrs lib.systems.flakeExposed fn; + + systemArgs = eachSystem (system: { + pkgs = ( + import inputs.nixpkgs { + inherit system; + } + ); + }); + + allPackages = importDir "${flake}/packages" ( + attrs: + lib.mapAttrs ( + name: entry: (if entry.type == "directory" then "${entry.path}/default.nix" else entry.path) + ) attrs + ); + + packages = + let + # TODO: Filter out packages that are not supported on the platform? + mkPackages = + system: + let + args = systemArgs."${system}"; + pkgs = args.pkgs; + in + lib.mapAttrs (name: package: pkgs.callPackage package { }) allPackages; + in + eachSystem mkPackages; + + overlay = final: prev: (lib.mapAttrs (name: package: prev.callPackage package { }) allPackages); + + collectEntries = + attrs: + lib.attrsets.collect ( + entry: (lib.isAttrs entry) && (lib.hasAttr "path" entry) && (lib.hasAttr "type" entry) + ) attrs; + + collectModules = + path: + importDirRecursive path ( + attrs: + map (entry: if entry.type == "directory" then entry.path + "/default.nix" else entry.path) ( + collectEntries attrs + ) + ); + + nixosModules = collectModules "${flake}/modules/nixos"; + inputNixosModules = lib.map (flake: flake.outputs.nixosModules.default) ( + lib.filter (flake: lib.hasAttrByPath [ "outputs" "nixosModules" "default" ] flake) ( + lib.attrValues inputs + ) + ); + + homeModules = collectModules "${flake}/modules/home"; + inputHomeModules = lib.map (flake: flake.outputs.homeManagerModules.default) ( + lib.filter (flake: lib.hasAttrByPath [ "outputs" "homeManagerModules" "default" ] flake) ( + lib.attrValues inputs + ) + ); + + inputOverlays = lib.map (flake: flake.outputs.overlays.default) ( + lib.filter (flake: lib.hasAttrByPath [ "outputs" "overlays" "default" ] flake) ( + lib.attrValues inputs + ) + ); + + overlayModule = + { ... }: + { + nixpkgs.overlays = [ overlay ] ++ inputOverlays; + }; + + nixosConfigurations = importDir "${flake}/hosts" ( + attrs: + lib.mapAttrs ( + name: entry: + lib.nixosSystem { + specialArgs = { + inherit inputs; + }; + modules = + let + systemPath = "${entry.path}/configuration.nix"; + + userEntries = importDir "${entry.path}/users" lib.id; + + usersConfiguration = lib.mapAttrs (name: entry: { + isNormalUser = true; + group = name; + }) userEntries; + groupsConfiguration = lib.mapAttrs (name: entry: { + }) userEntries; + homesConfiguration = lib.mapAttrs (name: entry: entry.path) userEntries; + + usersModule = + { ... }: + { + home-manager.sharedModules = homeModules ++ inputHomeModules; + home-manager.useUserPackages = false; # TODO: See if this should be changed to true? + home-manager.useGlobalPkgs = true; + home-manager.users = homesConfiguration; + users.users = usersConfiguration; + users.groups = groupsConfiguration; + }; + in + [ + systemPath + overlayModule + usersModule + ] + ++ nixosModules + ++ inputNixosModules; + } + ) (lib.attrsets.filterAttrs (name: entry: entry.type == "directory") attrs) + ); + +in +{ + inherit packages nixosConfigurations overlay; + overlays.default = overlay; +} diff --git a/glue/flake.nix b/glue/flake.nix deleted file mode 100644 index 2d1a609..0000000 --- a/glue/flake.nix +++ /dev/null @@ -1,14 +0,0 @@ -{ - description = "Some glue"; - - inputs = { }; - - outputs = - inputs: - let - glue = import ./lib { inherit inputs; }; - in - { - __functor = _: glue; - }; -} diff --git a/glue/lib/default.nix b/glue/lib/default.nix deleted file mode 100644 index 108a7c4..0000000 --- a/glue/lib/default.nix +++ /dev/null @@ -1,173 +0,0 @@ -{ ... }: - -let - mkGlue = - inputs: - let - flake = inputs.self; - nixpkgs = inputs.nixpkgs; - lib = nixpkgs.lib; - - importDir = - path: fn: - let - entries = builtins.readDir path; - - # Get paths to directories - dirs = lib.filterAttrs (_: type: type == "directory") entries; - dirPaths = lib.mapAttrs (name: type: { - path = "${path}/${name}"; - type = type; - }) dirs; - - # Get paths to nix files - nixName = name: builtins.match "(.*)\\.nix" name; - files = lib.filterAttrs (name: type: (type != "directory") && ((nixName name) != null)) entries; - filePaths = lib.mapAttrs' (name: type: { - name = builtins.head (nixName name); - value = { - path = "${path}/${name}"; - type = type; - }; - }) files; - - combined = dirPaths // filePaths; - in - fn (lib.optionalAttrs (builtins.pathExists path) combined); - - # Split out into getNixFiles, getNixFilesRecursive, getDirs - importDirRecursive = - path: fn: - let - entries = importDir path lib.id; - - # Dig down recursively - dirs = lib.filterAttrs (_: entry: entry.type == "directory") entries; - recursedEntries = lib.mapAttrs (name: entry: (importDirRecursive entry.path lib.id)) dirs; - in - fn (entries // recursedEntries); - - eachSystem = fn: lib.genAttrs lib.systems.flakeExposed fn; - - systemArgs = eachSystem (system: { - pkgs = ( - import inputs.nixpkgs { - inherit system; - } - ); - }); - - allPackages = importDir "${flake}/packages" ( - attrs: - lib.mapAttrs ( - name: entry: (if entry.type == "directory" then "${entry.path}/default.nix" else entry.path) - ) attrs - ); - - packages = - let - # TODO: Filter out packages that are not supported on the platform? - mkPackages = - system: - let - args = systemArgs."${system}"; - pkgs = args.pkgs; - in - lib.mapAttrs (name: package: pkgs.callPackage package { }) allPackages; - in - eachSystem mkPackages; - - overlay = final: prev: (lib.mapAttrs (name: package: prev.callPackage package { }) allPackages); - - collectEntries = - attrs: - lib.attrsets.collect ( - entry: (lib.isAttrs entry) && (lib.hasAttr "path" entry) && (lib.hasAttr "type" entry) - ) attrs; - - collectModules = - path: - importDirRecursive path ( - attrs: - map (entry: if entry.type == "directory" then entry.path + "/default.nix" else entry.path) ( - collectEntries attrs - ) - ); - - nixosModules = collectModules "${flake}/modules/nixos"; - inputNixosModules = lib.map (flake: flake.outputs.nixosModules.default) ( - lib.filter (flake: lib.hasAttrByPath [ "outputs" "nixosModules" "default" ] flake) ( - lib.attrValues inputs - ) - ); - - homeModules = collectModules "${flake}/modules/home"; - inputHomeModules = lib.map (flake: flake.outputs.homeManagerModules.default) ( - lib.filter (flake: lib.hasAttrByPath [ "outputs" "homeManagerModules" "default" ] flake) ( - lib.attrValues inputs - ) - ); - - inputOverlays = lib.map (flake: flake.outputs.overlays.default) ( - lib.filter (flake: lib.hasAttrByPath [ "outputs" "overlays" "default" ] flake) ( - lib.attrValues inputs - ) - ); - - overlayModule = - { ... }: - { - nixpkgs.overlays = [ overlay ] ++ inputOverlays; - }; - - nixosConfigurations = importDir "${flake}/hosts" ( - attrs: - lib.mapAttrs ( - name: entry: - lib.nixosSystem { - specialArgs = { - inherit inputs; - }; - modules = - let - systemPath = "${entry.path}/configuration.nix"; - - userEntries = importDir "${entry.path}/users" lib.id; - - usersConfiguration = lib.mapAttrs (name: entry: { - isNormalUser = true; - group = name; - }) userEntries; - groupsConfiguration = lib.mapAttrs (name: entry: { - }) userEntries; - homesConfiguration = lib.mapAttrs (name: entry: entry.path) userEntries; - - usersModule = - { ... }: - { - home-manager.sharedModules = homeModules ++ inputHomeModules; - home-manager.useUserPackages = false; # TODO: See if this should be changed to true? - home-manager.useGlobalPkgs = true; - home-manager.users = homesConfiguration; - users.users = usersConfiguration; - users.groups = groupsConfiguration; - }; - in - [ - systemPath - overlayModule - usersModule - ] - ++ nixosModules - ++ inputNixosModules; - } - ) (lib.attrsets.filterAttrs (name: entry: entry.type == "directory") attrs) - ); - - in - { - inherit packages nixosConfigurations; - overlays.default = overlay; - }; -in -mkGlue