refactor flake structure

This commit is contained in:
ulic-youthlic 2025-05-03 11:35:18 +08:00
parent 3412ecb175
commit 66a2700943
Signed by: youthlic
GPG key ID: 63E86C3C14A0D721
9 changed files with 207 additions and 177 deletions

2
.envrc
View file

@ -1,3 +1,3 @@
watch_file ./flake.nix ./flake.lock watch_file ./flake.lock **/*.nix
use flake use flake

View file

@ -14,17 +14,15 @@ Hey, you. This is my nixos configurations.
- david@Tytonidae - david@Tytonidae
| Specialisation | DE / WM | Shell | Editor | Terminal | Launcher | Browser | DM | | Specialisation | DE / WM | Shell | Editor | Terminal | Launcher | Browser | DM |
| -------------- | --------- | ----------- | ----------- | -------- | --------------- | ------- | ---------------- | | -------------- | ------- | ----------- | ----------- | -------- | -------- | ------- | ---------------- |
| default | niri | fish + bash | helix + zed | ghostty | fuzzel | firefox | greetd + regreet | | 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 |
- david@Akun - david@Akun
| Specialisation | DE / WM | Shell | Editor | Terminal | Launcher | Browser | DM | | 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 - alice@Cape

193
flake.nix
View file

@ -168,14 +168,21 @@
flake-parts, flake-parts,
flake-utils, flake-utils,
... ...
} @ inputs: let } @ inputs:
inherit (self) outputs; flake-parts.lib.mkFlake {inherit inputs;} ({flake-parts-lib, ...}: let
rootPath = ./.; inherit (self) outputs;
in inherit (flake-parts-lib) importApply;
flake-parts.lib.mkFlake {inherit inputs;} { 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; systems = flake-utils.lib.defaultSystems;
imports = [ imports = [
inputs.home-manager.flakeModules.home-manager inputs.home-manager.flakeModules.home-manager
nixos
home
deploy
]; ];
perSystem = { perSystem = {
pkgs, pkgs,
@ -204,164 +211,22 @@
]; ];
}; };
}; };
flake = flake = {
{ overlays = {
nix.settings = { modifications = import ./overlays/modifications {inherit inputs outputs;};
# substituters shared in home-manager and nixos configuration additions = import ./overlays/additions {inherit inputs outputs;};
substituters = let };
cachix = x: "https://${x}.cachix.org"; nix.settings = {
in # substituters shared in home-manager and nixos configuration
nixpkgs.lib.flatten [ substituters = let
(cachix "nix-community") cachix = x: "https://${x}.cachix.org";
"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;};
};
in in
nixosConfigDir nixpkgs.lib.flatten [
|> builtins.readDir (cachix "nix-community")
|> builtins.attrNames "https://cache.nixos.org"
|> map (f: nixpkgs.lib.removeSuffix ".nix" f) (cachix "cosmic")
|> 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) {};
}
);
};
} }

49
flake/deploy.nix Normal file
View file

@ -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) {};
};
}

79
flake/home.nix Normal file
View file

@ -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;
};
}

42
flake/nixos.nix Normal file
View file

@ -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;
};
}

View file

@ -1,6 +1,5 @@
{outputs, ...}: final: prev: let {outputs, ...}: final: prev: let
inherit (final) stdenv; inherit (prev.stdenv.hostPlatform) system;
inherit (stdenv.hostPlatform) system;
in { in {
rime-ice = outputs.packages."${system}".rime-ice; rime-ice = outputs.packages."${system}".rime-ice;
} }

View file

@ -1,6 +1,5 @@
{outputs, ...}: final: prev: let {outputs, ...}: final: prev: let
inherit (final) stdenv; inherit (prev.stdenv.hostPlatform) system;
inherit (stdenv.hostPlatform) system;
in { in {
juicity = outputs.packages."${system}".juicity; juicity = outputs.packages."${system}".juicity;
} }

View file

@ -1,6 +1,5 @@
{outputs, ...}: final: prev: let {outputs, ...}: final: prev: let
inherit (final) stdenv; inherit (prev.stdenv.hostPlatform) system;
inherit (stdenv.hostPlatform) system;
in { in {
spotify = outputs.packages."${system}".spotifyx; spotify = outputs.packages."${system}".spotifyx;
} }