From 66a27009434a97acb00651b0250c88958f9b829e Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Sat, 3 May 2025 11:35:18 +0800 Subject: [PATCH] refactor flake structure --- .envrc | 2 +- README.md | 10 +- flake.nix | 193 +++++----------------------- flake/deploy.nix | 49 +++++++ flake/home.nix | 79 ++++++++++++ flake/nixos.nix | 42 ++++++ overlays/additions/rime-ice.nix | 3 +- overlays/modifications/juicity.nix | 3 +- overlays/modifications/spotifyx.nix | 3 +- 9 files changed, 207 insertions(+), 177 deletions(-) create mode 100644 flake/deploy.nix create mode 100644 flake/home.nix create mode 100644 flake/nixos.nix diff --git a/.envrc b/.envrc index 7ad2f10..29e16c1 100644 --- a/.envrc +++ b/.envrc @@ -1,3 +1,3 @@ -watch_file ./flake.nix ./flake.lock +watch_file ./flake.lock **/*.nix use flake diff --git a/README.md b/README.md index cf7a4ea..6150021 100644 --- a/README.md +++ b/README.md @@ -14,17 +14,15 @@ Hey, you. This is my nixos configurations. - david@Tytonidae -| Specialisation | DE / WM | Shell | Editor | Terminal | Launcher | Browser | DM | -| -------------- | --------- | ----------- | ----------- | -------- | --------------- | ------- | ---------------- | -| default | niri | fish + bash | helix + zed | ghostty | fuzzel | firefox | greetd + regreet | -| cosmic | cosmic de | fish + bash | helix + zed | ghostty | cosmic-launcher | firefox | cosmic-greeter | -| kde | kde | fish + bash | helix + zed | ghostty | - | firefox | sddm | +| Specialisation | DE / WM | Shell | Editor | Terminal | Launcher | Browser | DM | +| -------------- | ------- | ----------- | ----------- | -------- | -------- | ------- | ---------------- | +| default | niri | fish + bash | helix + zed | ghostty | fuzzel | firefox | greetd + regreet | - david@Akun | Specialisation | DE / WM | Shell | Editor | Terminal | Launcher | Browser | DM | | -------------- | ------- | ----------- | ----------- | -------- | -------- | ------- | ---------------- | -| default | niri | fish + bash | helix + zed | ghostty | - | firefox | greetd + regreet | +| default | niri | fish + bash | helix + zed | ghostty | fuzzel | firefox | greetd + regreet | - alice@Cape diff --git a/flake.nix b/flake.nix index 5330054..2392f6c 100644 --- a/flake.nix +++ b/flake.nix @@ -168,14 +168,21 @@ flake-parts, flake-utils, ... - } @ inputs: let - inherit (self) outputs; - rootPath = ./.; - in - flake-parts.lib.mkFlake {inherit inputs;} { + } @ inputs: + flake-parts.lib.mkFlake {inherit inputs;} ({flake-parts-lib, ...}: let + inherit (self) outputs; + inherit (flake-parts-lib) importApply; + rootPath = ./.; + nixos = importApply ./flake/nixos.nix {inherit rootPath outputs;}; + home = importApply ./flake/home.nix {inherit rootPath outputs;}; + deploy = importApply ./flake/deploy.nix {inherit outputs;}; + in { systems = flake-utils.lib.defaultSystems; imports = [ inputs.home-manager.flakeModules.home-manager + nixos + home + deploy ]; perSystem = { pkgs, @@ -204,164 +211,22 @@ ]; }; }; - flake = - { - nix.settings = { - # substituters shared in home-manager and nixos configuration - substituters = let - cachix = x: "https://${x}.cachix.org"; - in - nixpkgs.lib.flatten [ - (cachix "nix-community") - "https://cache.nixos.org" - (cachix "cosmic") - ]; - }; - - nixosModules.default = import ./nixos/modules; - - overlays = { - modifications = import ./overlays/modifications {inherit inputs outputs;}; - additions = import ./overlays/additions {inherit inputs outputs;}; - }; - - nixosConfigurations = let - nixosConfigDir = ./nixos/configurations; - makeNixConfiguration = hostName: - nixpkgs.lib.nixosSystem { - modules = - [outputs.nixosModules.default] - ++ [ - ( - let - dirPath = nixosConfigDir + "/${hostName}"; - filePath = nixosConfigDir + "/${hostName}.nix"; - in - if builtins.pathExists dirPath - then dirPath - else filePath - ) - ]; - specialArgs = {inherit inputs outputs rootPath;}; - }; + flake = { + overlays = { + modifications = import ./overlays/modifications {inherit inputs outputs;}; + additions = import ./overlays/additions {inherit inputs outputs;}; + }; + nix.settings = { + # substituters shared in home-manager and nixos configuration + substituters = let + cachix = x: "https://${x}.cachix.org"; in - nixosConfigDir - |> builtins.readDir - |> builtins.attrNames - |> map (f: nixpkgs.lib.removeSuffix ".nix" f) - |> map (name: { - inherit name; - value = makeNixConfiguration name; - }) - |> builtins.listToAttrs; - } - // ( - let - mkHomeConfig = { - hostName, - unixName ? "david", - system ? "x86_64-linux", - nixpkgs ? inputs.nixpkgs, - home-manager ? inputs.home-manager, - }: { - "${unixName}@${hostName}" = home-manager.lib.homeManagerConfiguration { - pkgs = nixpkgs.legacyPackages."${system}"; - modules = - [ - "${toString ./home}/${unixName}/configurations/${hostName}" - ] - ++ (with outputs.homeModules; [ - default - extra - ]) - ++ [ - outputs.homeModules."${unixName}" - ]; - extraSpecialArgs = { - inherit - inputs - outputs - unixName - hostName - system - rootPath - ; - }; - }; - }; - in { - homeConfigurations = - nixpkgs.lib.foldr (a: b: a // b) {} ( - map (hostName: mkHomeConfig {inherit hostName;}) [ - "Tytonidae" - "Akun" - ] - ) - // mkHomeConfig { - hostName = "Cape"; - unixName = "alice"; - }; - homeModules = - { - default = import ./home/modules; - extra = import ./home/extra; - } - // ( - ./home - |> builtins.readDir - |> nixpkgs.lib.filterAttrs (key: value: value == "directory") - |> nixpkgs.lib.filterAttrs ( - key: value: - !builtins.elem key [ - "modules" - "extra" - ] - ) - |> builtins.attrNames - |> map (name: { - name = name; - value = import "${toString ./home}/${name}/modules"; - }) - |> builtins.listToAttrs - ); - } - ) - // ( - let - mkDeployNode = { - hostName, - unixName ? "deploy", - system ? "x86_64-linux", - sshName ? hostName, - }: { - "${hostName}" = { - hostname = "${sshName}"; - sshUser = "${unixName}"; - interactiveSudo = true; - profiles = { - system = { - user = "root"; - path = - inputs.deploy-rs.lib."${system}".activate.nixos - self.outputs.nixosConfigurations."${hostName}"; - }; - }; - }; - }; - in { - deploy.nodes = - [ - "Cape" - "Akun" - ] - |> map ( - hostName: - mkDeployNode { - inherit hostName; - } - ) - |> nixpkgs.lib.foldr (a: b: a // b) {}; - } - ); - }; + nixpkgs.lib.flatten [ + (cachix "nix-community") + "https://cache.nixos.org" + (cachix "cosmic") + ]; + }; + }; + }); } diff --git a/flake/deploy.nix b/flake/deploy.nix new file mode 100644 index 0000000..5741f9b --- /dev/null +++ b/flake/deploy.nix @@ -0,0 +1,49 @@ +{outputs}: { + lib, + inputs, + flake-parts-lib, + ... +}: let + mkDeployNode = { + hostName, + unixName ? "deploy", + system ? "x86_64-linux", + sshName ? hostName, + }: { + "${hostName}" = { + hostname = "${sshName}"; + sshUser = "${unixName}"; + interactiveSudo = true; + profiles = { + system = { + user = "root"; + path = + inputs.deploy-rs.lib."${system}".activate.nixos + outputs.nixosConfigurations."${hostName}"; + }; + }; + }; + }; +in { + options = { + flake = flake-parts-lib.mkSubmoduleOptions { + deploy = lib.mkOption { + type = lib.types.lazyAttrsOf lib.types.raw; + }; + }; + }; + config = { + flake.deploy.nodes = + [ + "Cape" + "Akun" + ] + |> map ( + hostName: + mkDeployNode { + inherit hostName; + } + ) + |> lib.foldr (a: b: a // b) {}; + }; +} diff --git a/flake/home.nix b/flake/home.nix new file mode 100644 index 0000000..43024c1 --- /dev/null +++ b/flake/home.nix @@ -0,0 +1,79 @@ +{ + outputs, + rootPath, +}: { + lib, + inputs, + ... +}: let + homeModules = + { + default = import "${toString rootPath}/home/modules"; + extra = import "${toString rootPath}/home/extra"; + } + // ( + (rootPath + "/home") + |> builtins.readDir + |> lib.filterAttrs (key: value: value == "directory") + |> lib.filterAttrs ( + key: value: + !builtins.elem key [ + "modules" + "extra" + ] + ) + |> builtins.attrNames + |> map (name: { + name = name; + value = import "${toString rootPath}/home/${name}/modules"; + }) + |> builtins.listToAttrs + ); + mkHomeConfig = { + hostName, + unixName ? "david", + system ? "x86_64-linux", + nixpkgs ? inputs.nixpkgs, + home-manager ? inputs.home-manager, + }: { + "${unixName}@${hostName}" = home-manager.lib.homeManagerConfiguration { + pkgs = nixpkgs.legacyPackages."${system}"; + modules = + [ + "${toString rootPath}/home/${unixName}/configurations/${hostName}" + ] + ++ (with homeModules; [ + default + extra + ]) + ++ [ + homeModules."${unixName}" + ]; + extraSpecialArgs = { + inherit + inputs + outputs + unixName + hostName + system + rootPath + ; + }; + }; + }; +in { + flake = { + homeConfigurations = + lib.foldr (a: b: a // b) {} ( + map (hostName: mkHomeConfig {inherit hostName;}) [ + "Tytonidae" + "Akun" + ] + ) + // mkHomeConfig { + hostName = "Cape"; + unixName = "alice"; + }; + inherit homeModules; + }; +} diff --git a/flake/nixos.nix b/flake/nixos.nix new file mode 100644 index 0000000..519cab5 --- /dev/null +++ b/flake/nixos.nix @@ -0,0 +1,42 @@ +{ + rootPath, + outputs, +}: {inputs, ...}: let + defaultNixosModule = import (rootPath + "/nixos/modules"); + inherit (inputs.nixpkgs) lib; +in { + flake = { + nixosModules.default = defaultNixosModule; + nixosConfigurations = let + nixosConfigDir = rootPath + "/nixos/configurations"; + makeNixConfiguration = hostName: + lib.nixosSystem { + modules = + [defaultNixosModule] + ++ [ + ( + let + dirPath = nixosConfigDir + "/${hostName}"; + filePath = nixosConfigDir + "/${hostName}.nix"; + in + if builtins.pathExists dirPath + then dirPath + else filePath + ) + ]; + specialArgs = { + inherit inputs outputs rootPath; + }; + }; + in + nixosConfigDir + |> builtins.readDir + |> builtins.attrNames + |> map (f: lib.removeSuffix ".nix" f) + |> map (name: { + inherit name; + value = makeNixConfiguration name; + }) + |> builtins.listToAttrs; + }; +} diff --git a/overlays/additions/rime-ice.nix b/overlays/additions/rime-ice.nix index da0c133..a54e748 100644 --- a/overlays/additions/rime-ice.nix +++ b/overlays/additions/rime-ice.nix @@ -1,6 +1,5 @@ {outputs, ...}: final: prev: let - inherit (final) stdenv; - inherit (stdenv.hostPlatform) system; + inherit (prev.stdenv.hostPlatform) system; in { rime-ice = outputs.packages."${system}".rime-ice; } diff --git a/overlays/modifications/juicity.nix b/overlays/modifications/juicity.nix index 52f5127..61c970c 100644 --- a/overlays/modifications/juicity.nix +++ b/overlays/modifications/juicity.nix @@ -1,6 +1,5 @@ {outputs, ...}: final: prev: let - inherit (final) stdenv; - inherit (stdenv.hostPlatform) system; + inherit (prev.stdenv.hostPlatform) system; in { juicity = outputs.packages."${system}".juicity; } diff --git a/overlays/modifications/spotifyx.nix b/overlays/modifications/spotifyx.nix index da92346..820b1d3 100644 --- a/overlays/modifications/spotifyx.nix +++ b/overlays/modifications/spotifyx.nix @@ -1,6 +1,5 @@ {outputs, ...}: final: prev: let - inherit (final) stdenv; - inherit (stdenv.hostPlatform) system; + inherit (prev.stdenv.hostPlatform) system; in { spotify = outputs.packages."${system}".spotifyx; }