From e5a75cb3b742ae8a4a226657a0dd8b9b9a3c459a Mon Sep 17 00:00:00 2001 From: soraefir Date: Sat, 20 Jun 2026 12:50:42 +0200 Subject: [PATCH] Multi system and pkgs cleanup --- flake.nix | 38 ++++--- generator.nix | 142 ++++++++++++++++----------- modules/nixos/system/nix/default.nix | 6 +- modules/shared/syscfg/default.nix | 21 ++-- overlays/default.nix | 5 +- shells/default.nix | 16 ++- systems/asgard/cfg.nix | 1 + 7 files changed, 131 insertions(+), 98 deletions(-) diff --git a/flake.nix b/flake.nix index 8e26870..1c0dbb1 100755 --- a/flake.nix +++ b/flake.nix @@ -39,25 +39,35 @@ outputs = inputs: let lib = inputs.nixpkgs.lib; - gen = import ./generator.nix { inherit inputs; }; + linuxSystems = [ "x86_64-linux" "aarch64-linux" ]; + supportedSystems = linuxSystems ++ [ "x86_64-darwin" "aarch64-darwin" ]; + overlays = import ./overlays { inherit inputs; }; + mkPkgs = system: + import inputs.nixpkgs { + inherit system; + inherit overlays; + config.allowUnfree = true; + }; + gen = import ./generator.nix { inherit inputs mkPkgs supportedSystems; }; systemsDir = ./systems; isIgnoredSystemDir = name: lib.hasPrefix "_" name || lib.hasPrefix "." name; - systemNames = lib.attrNames (lib.filterAttrs - (name: type: + systemNames = lib.attrNames ( + lib.filterAttrs ( + name: type: type == "directory" && !isIgnoredSystemDir name - && builtins.pathExists (systemsDir + "/${name}/cfg.nix")) - (builtins.readDir systemsDir)); + && builtins.pathExists (systemsDir + "/${name}/cfg.nix") + ) (builtins.readDir systemsDir) + ); hostsByType = systemType: - lib.filter - (host: (import (systemsDir + "/${host}/cfg.nix")).syscfg.type == systemType) - systemNames; + lib.filter (host: ((import (systemsDir + "/${host}/cfg.nix")).syscfg.type or "nixos") == systemType) systemNames; generateHosts = systemType: - lib.genAttrs - (hostsByType systemType) - (host: gen.generate { inherit host; }); + lib.genAttrs (hostsByType systemType) (host: gen.generate { inherit host; }); in { - devShells = import ./shells { inherit inputs; }; + devShells = import ./shells { + inherit inputs mkPkgs; + supportedSystems = linuxSystems; + }; nixosConfigurations = generateHosts "nixos"; darwinConfigurations = generateHosts "macos"; @@ -76,8 +86,8 @@ # diyu - ? # tirnanog - ? # valhalla - ? - # arcadia - ? - # elysium - ? + # arcadia - ? + # elysium - ? # empyrean - ? # duat - ? # sheol - ? diff --git a/generator.nix b/generator.nix index ef7dafc..8a1bca8 100755 --- a/generator.nix +++ b/generator.nix @@ -1,70 +1,92 @@ -{ inputs, ... }: { +{ inputs, mkPkgs, supportedSystems, ... }: +{ generate = { host }: let syscfg = import ./systems/${host}/cfg.nix; nameValuePair = name: value: { inherit name value; }; - in ({ - "nixos" = inputs.nixpkgs.lib.nixosSystem { - system = "x86_64-linux"; - specialArgs = { inherit inputs; }; - modules = [ + systemType = syscfg.syscfg.type or "nixos"; + defaultSystem = + { + nixos = "x86_64-linux"; + macos = "x86_64-darwin"; + home = "x86_64-linux"; + } + .${systemType} or (throw "Unsupported system type: ${systemType}"); + requestedSystem = syscfg.syscfg.system or defaultSystem; + system = + if builtins.elem requestedSystem supportedSystems then + requestedSystem + else + throw "Unsupported system '${requestedSystem}' for host '${host}'"; + users = syscfg.syscfg.users or [ ]; + mkLinuxHomeImports = + userConfig: + [ ./modules/shared/syscfg - ./modules/shared/sops - ./modules/nixos + ./modules/shared/colors + ./modules/home syscfg - ./systems/${host} - inputs.sops-nix.nixosModules.sops - inputs.home-manager.nixosModules.home-manager - - inputs.nixos-wsl.nixosModules.wsl - inputs.vscode-server.nixosModules.default - - { - home-manager.useGlobalPkgs = true; - home-manager.useUserPackages = true; - home-manager.extraSpecialArgs = { inherit inputs; }; - home-manager.users = builtins.listToAttrs (map (userConfig: - nameValuePair userConfig.username { - imports = [ - ./modules/shared/syscfg - ./modules/shared/colors - ./modules/home - syscfg - { usercfg = userConfig; } - inputs.nix-colors.homeManagerModule - inputs.sops-nix.homeManagerModules.sops - ]; - }) syscfg.syscfg.users); - } + { usercfg = userConfig; } + inputs.nix-colors.homeManagerModule + inputs.sops-nix.homeManagerModules.sops ]; + mkDarwinHomeImports = [ inputs.nix-colors.homeManagerModule inputs.sops-nix.homeManagerModules.sops ]; + mkHomeUsers = importsForUser: + builtins.listToAttrs ( + map (userConfig: nameValuePair userConfig.username { imports = importsForUser userConfig; }) users + ); + mkHomeManager = importsForUser: { + home-manager.useGlobalPkgs = true; + home-manager.useUserPackages = true; + home-manager.extraSpecialArgs = { inherit inputs; }; + home-manager.users = mkHomeUsers importsForUser; }; + defaultUser = + builtins.head ( + builtins.filter (user: user.username == syscfg.syscfg.defaultUser) users + ++ [ { username = syscfg.syscfg.defaultUser; } ] + ); + in + ( + { + "nixos" = inputs.nixpkgs.lib.nixosSystem { + inherit system; + specialArgs = { inherit inputs; }; + modules = [ + ./modules/shared/syscfg + ./modules/shared/sops + ./modules/nixos + syscfg + ./systems/${host} + inputs.sops-nix.nixosModules.sops + inputs.home-manager.nixosModules.home-manager - "macos" = inputs.darwin.lib.darwinSystem { - system = "x86_64-darwin"; - modules = [ - ./modules/shared/syscfg - ./modules/shared/sops - syscfg - ./systems/${host} - inputs.sops-nix.nixosModules.sops - inputs.home-manager.darwinModules.home-manager - { - home-manager.useGlobalPkgs = true; - home-manager.useUserPackages = true; - home-manager.extraSpecialArgs = { inherit inputs; }; - home-manager.users = builtins.listToAttrs (map (userConfig: - nameValuePair userConfig.username { - imports = [ - inputs.nix-colors.homeManagerModule - inputs.sops-nix.homeManagerModules.sops - ]; - }) syscfg.syscfg.users); - } - ]; - }; - "home" = inputs.home-manager.lib.homeManagerConfiguration { - modules = [ ./modules/home ]; - }; - _ = throw "Unsupported system"; - }.${syscfg.syscfg.type}); + inputs.nixos-wsl.nixosModules.wsl + inputs.vscode-server.nixosModules.default + + (mkHomeManager mkLinuxHomeImports) + ]; + }; + + "macos" = inputs.darwin.lib.darwinSystem { + inherit system; + modules = [ + ./modules/shared/syscfg + ./modules/shared/sops + syscfg + ./systems/${host} + inputs.sops-nix.darwinModules.sops + inputs.home-manager.darwinModules.home-manager + (mkHomeManager (_: mkDarwinHomeImports)) + ]; + }; + "home" = inputs.home-manager.lib.homeManagerConfiguration { + pkgs = mkPkgs system; + extraSpecialArgs = { inherit inputs; }; + modules = mkLinuxHomeImports defaultUser; + }; + _ = throw "Unsupported system"; + } + .${systemType} + ); } diff --git a/modules/nixos/system/nix/default.nix b/modules/nixos/system/nix/default.nix index 8d791e4..883aa78 100644 --- a/modules/nixos/system/nix/default.nix +++ b/modules/nixos/system/nix/default.nix @@ -3,12 +3,8 @@ permittedInsecurePackages = [ ]; allowUnfree = true; android_sdk.accept_license = true; - packageOverrides = pkgs: rec { - custom = import ../../../../packages { inherit pkgs; }; - }; - }; - nixpkgs.overlays = import ../../../../overlays { inherit inputs pkgs; }; + nixpkgs.overlays = import ../../../../overlays { inherit inputs; }; nix = { package = pkgs.nixVersions.stable; extraOptions = '' diff --git a/modules/shared/syscfg/default.nix b/modules/shared/syscfg/default.nix index f10e8a1..54d3266 100644 --- a/modules/shared/syscfg/default.nix +++ b/modules/shared/syscfg/default.nix @@ -1,11 +1,15 @@ -{ inputs, lib, ... }: +{ lib, ... }: let systemsDir = ../../../systems; - systemNames = lib.attrNames (lib.filterAttrs - (name: type: type == "directory" && builtins.pathExists (systemsDir + "/${name}/cfg.nix")) - (builtins.readDir systemsDir)); - - + isIgnoredSystemDir = name: lib.hasPrefix "_" name || lib.hasPrefix "." name; + systemNames = lib.attrNames ( + lib.filterAttrs + (name: type: + type == "directory" + && !isIgnoredSystemDir name + && builtins.pathExists (systemsDir + "/${name}/cfg.nix")) + (builtins.readDir systemsDir) + ); in with lib; { options.usercfg = import ./user.nix {inherit lib;}; options.syscfg = { @@ -14,6 +18,11 @@ in with lib; { type = types.enum [ "nixos" "macos" "home" ]; default = "nixos"; }; + system = mkOption { + type = types.enum [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; + default = "x86_64-linux"; + description = "Nix platform used to evaluate this host."; + }; defaultUser = mkOption { type = types.str; }; make = import ./make.nix {inherit lib;}; net = import ./net.nix {inherit lib;}; diff --git a/overlays/default.nix b/overlays/default.nix index bccea12..0d70fee 100644 --- a/overlays/default.nix +++ b/overlays/default.nix @@ -1,15 +1,14 @@ -{ inputs, pkgs, ... }: +{ inputs, ... }: [ (final: prev: { #openttd-jgrpp = import ./openttd-jgrpp { inherit final prev; }; #yarn-berry = import ./yarn-berry { inherit final prev; }; #eww = import ./eww { inherit final prev; }; # ags = import ./ags { inherit final prev; }; + custom = import ../packages { pkgs = final; }; wine = final.unstable.wineWow64Packages.unstableFull; unstable = import inputs.nixUnstable { - system = final.stdenv.hostPlatform.system; - stdenv.hostPlatform.system = final.stdenv.hostPlatform.system; config.allowUnfree = true; }; }) diff --git a/shells/default.nix b/shells/default.nix index 67d79d1..3160886 100644 --- a/shells/default.nix +++ b/shells/default.nix @@ -1,13 +1,9 @@ -{ inputs, ... }: -let - forEachSystem = - inputs.nixpkgs.lib.genAttrs [ "aarch64-linux" "x86_64-linux" ]; -in forEachSystem (system: - let - overlays = import ../overlays { inherit inputs pkgs; }; - overrides = { custom = import ../packages { inherit pkgs; }; }; - pkgs = import inputs.nixpkgs { inherit system overlays; } // overrides; +{ inputs, mkPkgs, supportedSystems, ... }: +inputs.nixpkgs.lib.genAttrs supportedSystems ( + system: + let pkgs = mkPkgs system; in { default = import ./devsh { inherit pkgs; }; devsh = import ./devsh { inherit pkgs; }; - }) + } +) diff --git a/systems/asgard/cfg.nix b/systems/asgard/cfg.nix index d78e0b1..1848cca 100644 --- a/systems/asgard/cfg.nix +++ b/systems/asgard/cfg.nix @@ -3,5 +3,6 @@ hostname = "asgard"; defaultUser = "sora"; type = "macos"; + system = "x86_64-darwin"; }; }