diff --git a/flake.nix b/flake.nix index 8e26870..928065a 100755 --- a/flake.nix +++ b/flake.nix @@ -36,28 +36,79 @@ }; }; - outputs = inputs: + outputs = + inputs: let lib = inputs.nixpkgs.lib; - gen = import ./generator.nix { inherit inputs; }; + supportedSystems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + ]; + linuxSystems = [ + "x86_64-linux" + "aarch64-linux" + ]; + forEachSystem = lib.genAttrs; + overlays = import ./overlays { inherit inputs; }; + mkPkgs = + system: + import inputs.nixpkgs { + inherit system; + overlays = overlays ++ [ + (final: prev: { + custom = import ./packages { pkgs = final; }; + }) + ]; + 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)); - hostsByType = systemType: - lib.filter - (host: (import (systemsDir + "/${host}/cfg.nix")).syscfg.type == systemType) - systemNames; - generateHosts = systemType: - lib.genAttrs - (hostsByType systemType) - (host: gen.generate { inherit host; }); - in { - devShells = import ./shells { inherit inputs; }; + && builtins.pathExists (systemsDir + "/${name}/cfg.nix") + ) (builtins.readDir systemsDir) + ); + hostsByType = + systemType: + lib.filter (host: (import (systemsDir + "/${host}/cfg.nix")).syscfg.type == systemType) systemNames; + generateHosts = + systemType: lib.genAttrs (hostsByType systemType) (host: gen.generate { inherit host; }); + flattenPackages = + prefix: attrs: + lib.concatMapAttrs ( + name: value: + let + packageName = lib.concatStringsSep "-" (prefix ++ [ name ]); + defaultPackageName = lib.concatStringsSep "-" prefix; + in + if lib.isDerivation value then + { + "${if name == "default" && prefix != [ ] then defaultPackageName else packageName}" = value; + } + else if lib.isAttrs value then + flattenPackages (if name == "default" then prefix else prefix ++ [ name ]) value + else + { } + ) attrs; + mkPackages = + system: + let + pkgs = mkPkgs system; + in + lib.optionalAttrs pkgs.stdenv.isLinux (flattenPackages [ ] pkgs.custom); + in + { + packages = forEachSystem linuxSystems mkPackages; + devShells = import ./shells { + inherit inputs mkPkgs; + supportedSystems = linuxSystems; + }; nixosConfigurations = generateHosts "nixos"; darwinConfigurations = generateHosts "macos"; @@ -76,8 +127,8 @@ # diyu - ? # tirnanog - ? # valhalla - ? - # arcadia - ? - # elysium - ? + # arcadia - ? + # elysium - ? # empyrean - ? # duat - ? # sheol - ? diff --git a/generator.nix b/generator.nix index ef7dafc..d904367 100755 --- a/generator.nix +++ b/generator.nix @@ -1,70 +1,105 @@ -{ inputs, ... }: { - generate = { host }: +{ + 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 = [ - ./modules/shared/syscfg - ./modules/shared/sops - ./modules/nixos - syscfg - ./systems/${host} - inputs.sops-nix.nixosModules.sops - inputs.home-manager.nixosModules.home-manager + 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}"); + system = syscfg.syscfg.system or defaultSystem; + checkedSystem = + if builtins.elem system supportedSystems then + system + else + throw "Unsupported system '${system}' for host '${host}'"; + pkgs = mkPkgs checkedSystem; + in + ( + { + "nixos" = inputs.nixpkgs.lib.nixosSystem { + system = checkedSystem; + 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 - inputs.nixos-wsl.nixosModules.wsl - inputs.vscode-server.nixosModules.default + 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); - } - ]; - }; + { + 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 or [ ]) + ); + } + ]; + }; - "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}); + "macos" = inputs.darwin.lib.darwinSystem { + system = checkedSystem; + modules = [ + ./modules/shared/syscfg + ./modules/shared/sops + syscfg + ./systems/${host} + inputs.sops-nix.darwinModules.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 or [ ]) + ); + } + ]; + }; + "home" = inputs.home-manager.lib.homeManagerConfiguration { + inherit pkgs; + modules = [ ./modules/home ]; + }; + _ = throw "Unsupported system"; + } + .${systemType} + ); } diff --git a/modules/server/containers/builder.nix b/modules/server/containers/builder.nix index 985fdac..66a58e1 100644 --- a/modules/server/containers/builder.nix +++ b/modules/server/containers/builder.nix @@ -61,7 +61,7 @@ let }; in lib.recursiveUpdate base overrides; vmBuilder = { name, vm }: ((import "${pkgs.path}/nixos/lib/eval-config.nix" { - system = "x86_64-linux"; + system = pkgs.stdenv.hostPlatform.system; modules = [ vm.cfg ({ config, lib, modulesPath, ... }: { imports = [ diff --git a/modules/shared/sops/default.nix b/modules/shared/sops/default.nix index c0e96e0..4c2e650 100755 --- a/modules/shared/sops/default.nix +++ b/modules/shared/sops/default.nix @@ -1,6 +1,8 @@ { config, lib, pkgs, ... }: let isCI = builtins.elem config.syscfg.hostname [ "ci" "sandbox" ]; + defaultUser = config.users.users.${config.syscfg.defaultUser} or { }; + defaultGroup = if pkgs.stdenv.isDarwin then "staff" else "users"; keyFilePath = (if isCI then "/var/lib/sops-nix/mock-key.txt" else @@ -8,7 +10,7 @@ let sopsFilePath = (if isCI then ./mock.yaml else ./common.yaml); in { environment.systemPackages = with pkgs; [ sops ]; - environment.sessionVariables.SOPS_AGE_KEY_FILE = keyFilePath; + environment.variables.SOPS_AGE_KEY_FILE = keyFilePath; sops.defaultSopsFile = sopsFilePath; sops.age.keyFile = keyFilePath; @@ -19,8 +21,8 @@ in { { "${config.syscfg.hostname}_ssh_priv" = { mode = "0400"; - owner = config.users.users.${config.syscfg.defaultUser}.name; - group = config.users.users.${config.syscfg.defaultUser}.group; + owner = defaultUser.name or config.syscfg.defaultUser; + group = defaultUser.group or defaultGroup; }; } (lib.mkIf config.syscfg.net.wlp.enable { diff --git a/modules/shared/syscfg/default.nix b/modules/shared/syscfg/default.nix index f10e8a1..6b08db8 100644 --- a/modules/shared/syscfg/default.nix +++ b/modules/shared/syscfg/default.nix @@ -14,6 +14,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..5603d75 100644 --- a/overlays/default.nix +++ b/overlays/default.nix @@ -1,4 +1,4 @@ -{ inputs, pkgs, ... }: +{ inputs, ... }: [ (final: prev: { #openttd-jgrpp = import ./openttd-jgrpp { inherit final prev; }; @@ -7,9 +7,7 @@ # ags = import ./ags { inherit final prev; }; 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/packages/default.nix b/packages/default.nix index 5f71d85..8965d62 100644 --- a/packages/default.nix +++ b/packages/default.nix @@ -1,4 +1,5 @@ -{ pkgs, ... }: { +{ pkgs, ... }: +{ amdgpu_top = pkgs.callPackage ./amdgpu_top { }; simc = pkgs.qt6.callPackage ./simc { }; repalette = pkgs.callPackage ./repalette { }; diff --git a/shells/default.nix b/shells/default.nix index 67d79d1..cf1e51a 100644 --- a/shells/default.nix +++ b/shells/default.nix @@ -1,13 +1,19 @@ -{ inputs, ... }: +{ + inputs, + mkPkgs, + supportedSystems, + ... +}: let - forEachSystem = - inputs.nixpkgs.lib.genAttrs [ "aarch64-linux" "x86_64-linux" ]; -in forEachSystem (system: + forEachSystem = inputs.nixpkgs.lib.genAttrs supportedSystems; +in +forEachSystem ( + system: let - overlays = import ../overlays { inherit inputs pkgs; }; - overrides = { custom = import ../packages { inherit pkgs; }; }; - pkgs = import inputs.nixpkgs { inherit system overlays; } // overrides; - in { + 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"; }; } diff --git a/systems/asgard/default.nix b/systems/asgard/default.nix index eb7983e..86c04e9 100755 --- a/systems/asgard/default.nix +++ b/systems/asgard/default.nix @@ -12,8 +12,7 @@ }; fonts = { - fontDir.enable = true; - fonts = with pkgs; [ ibm-plex openmoji-color material-design-icons ]; + packages = with pkgs; [ ibm-plex openmoji-color material-design-icons ]; }; environment = { @@ -33,8 +32,6 @@ programs = { zsh.enable = true; }; - services = { nix-daemon.enable = true; }; - homebrew = { enable = true; onActivation = { @@ -54,7 +51,11 @@ ''; gc = { automatic = true; - dates = "weekly"; + interval = { + Weekday = 0; + Hour = 3; + Minute = 0; + }; options = "--delete-older-than 7d"; }; settings = { @@ -67,6 +68,7 @@ }; system = { + primaryUser = "sora"; defaults = { NSGlobalDomain = { KeyRepeat = 1;