Refactor flake structure with flake-parts

This commit is contained in:
ulic-youthlic 2025-06-09 16:14:57 +08:00
parent 8ca6499da1
commit cf83a45191
Signed by: youthlic
GPG key ID: 63E86C3C14A0D721
10 changed files with 203 additions and 176 deletions

157
flake.nix
View file

@ -1,6 +1,37 @@
{ {
description = "A simple NixOS flakes"; description = "A simple NixOS flakes";
outputs = {
nixpkgs,
flake-parts,
flake-utils,
home-manager,
treefmt-nix,
...
} @ inputs:
flake-parts.lib.mkFlake {inherit inputs;} {
systems = flake-utils.lib.defaultSystems;
imports = [
home-manager.flakeModules.home-manager
treefmt-nix.flakeModule
./flake
];
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")
];
};
};
};
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
# nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small"; # nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small";
@ -140,130 +171,4 @@
repo = "treefmt-nix"; repo = "treefmt-nix";
}; };
}; };
outputs = {
self,
nixpkgs,
flake-parts,
flake-utils,
...
} @ 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;};
templates = importApply ./flake/templates.nix {inherit rootPath;};
in {
systems = flake-utils.lib.defaultSystems;
imports = [
inputs.home-manager.flakeModules.home-manager
inputs.treefmt-nix.flakeModule
nixos
home
deploy
templates
];
perSystem = {
pkgs,
system,
...
} @ args: {
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
config = {
allowUnfree = true;
};
};
packages = import ./pkgs (
args
// {
inherit inputs rootPath;
}
);
devShells.default = pkgs.mkShell {
packages = with pkgs; [
nixd
typos
typos-lsp
just
nvfetcher
];
};
treefmt = {
programs = {
alejandra = {
enable = true;
excludes = ["pkgs/_sources/*.nix"];
};
biome = {
enable = true;
includes = ["*.json"];
excludes = ["pkgs/_sources/*.json"];
settings = {
javascript.formatter.enabled = false;
css.formatter.enabled = false;
};
};
dprint = {
enable = true;
includes = ["*.md" "*.toml" "*.yaml"];
excludes = ["secrets/*.yaml"];
settings = {
plugins = pkgs.dprint-plugins.getPluginList (plugins:
with plugins; [
dprint-plugin-toml
dprint-plugin-markdown
g-plane-pretty_yaml
]);
};
};
just = {
enable = true;
includes = [".justfile"];
};
typos = let
config = ./.typos.toml |> builtins.readFile |> builtins.fromTOML;
in {
enable = true;
includes = ["*"];
excludes = ["assets/*"] ++ config.files.extend-exclude;
configFile = "${toString ./.typos.toml}";
# Disable all extra option in treefmt module.
# Use config file.
sort = false;
isolated = false;
hidden = false;
noIgnore = false;
noIgnoreDot = false;
noIgnoreGlobal = false;
noIgnoreParent = false;
noIgnoreVCS = false;
binary = false;
noCheckFilenames = false;
noCheckFiles = false;
noUnicode = false;
};
};
};
};
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
nixpkgs.lib.flatten [
(cachix "nix-community")
"https://cache.nixos.org"
(cachix "cosmic")
];
};
};
});
} }

10
flake/default.nix Normal file
View file

@ -0,0 +1,10 @@
{
imports = [
./nixos.nix
./home.nix
./templates.nix
./perSystem.nix
./deploy.nix
./overlays.nix
];
}

View file

@ -1,9 +1,12 @@
{outputs}: { {
lib, lib,
inputs, inputs,
flake-parts-lib, flake-parts-lib,
self,
... ...
}: let }: let
inherit (self) outputs;
inherit (inputs) deploy-rs;
mkDeployNode = { mkDeployNode = {
hostName, hostName,
unixName ? "deploy", unixName ? "deploy",
@ -18,7 +21,7 @@
system = { system = {
user = "root"; user = "root";
path = path =
inputs.deploy-rs.lib."${system}".activate.nixos deploy-rs.lib."${system}".activate.nixos
outputs.nixosConfigurations."${hostName}"; outputs.nixosConfigurations."${hostName}";
}; };
}; };

View file

@ -1,17 +1,13 @@
{ {
outputs,
rootPath,
}: {
lib, lib,
inputs, inputs,
self,
... ...
}: let }: let
rootPath = ./..;
inherit (self) outputs;
homeModules = homeModules =
{ (
default = import "${toString rootPath}/home/modules";
extra = import "${toString rootPath}/home/extra";
}
// (
(rootPath + "/home") (rootPath + "/home")
|> builtins.readDir |> builtins.readDir
|> lib.filterAttrs (key: value: value == "directory") |> lib.filterAttrs (key: value: value == "directory")
@ -23,13 +19,13 @@
] ]
) )
|> builtins.attrNames |> builtins.attrNames
|> map (name: { |> (with lib; flip genAttrs (name: import (rootPath + "/home/${name}/modules")))
name = name; )
value = import "${toString rootPath}/home/${name}/modules"; // {
}) default = import "${toString rootPath}/home/modules";
|> builtins.listToAttrs extra = import "${toString rootPath}/home/extra";
); };
mkHomeConfig = { makeHomeConfiguration = {
hostName, hostName,
unixName ? "david", unixName ? "david",
system ? "x86_64-linux", system ? "x86_64-linux",
@ -37,10 +33,12 @@
home-manager ? inputs.home-manager, home-manager ? inputs.home-manager,
}: { }: {
"${unixName}@${hostName}" = home-manager.lib.homeManagerConfiguration { "${unixName}@${hostName}" = home-manager.lib.homeManagerConfiguration {
pkgs = nixpkgs.legacyPackages."${system}"; pkgs = import nixpkgs {
inherit system;
};
modules = modules =
[ [
"${toString rootPath}/home/${unixName}/configurations/${hostName}" (rootPath + "/home/${unixName}/configurations/${hostName}")
] ]
++ (with homeModules; [ ++ (with homeModules; [
default default
@ -67,7 +65,7 @@ in {
[ [
# Hostname # Hostname
] ]
|> map (hostName: mkHomeConfig {inherit hostName;}) |> map (hostName: makeHomeConfiguration {inherit hostName;})
); );
inherit homeModules; inherit homeModules;
}; };

View file

@ -1,42 +1,37 @@
{ {
rootPath, inputs,
outputs, lib,
}: {inputs, ...}: let self,
...
}: let
rootPath = ./..;
inherit (self) outputs;
inherit (inputs) nixpkgs;
defaultNixosModule = import (rootPath + "/nixos/modules"); defaultNixosModule = import (rootPath + "/nixos/modules");
inherit (inputs.nixpkgs) lib;
in { in {
flake = { flake = {
nixosModules.default = defaultNixosModule; nixosModules.default = defaultNixosModule;
nixosConfigurations = let nixosConfigurations = let
nixosConfigDir = rootPath + "/nixos/configurations";
makeNixConfiguration = hostName: makeNixConfiguration = hostName:
lib.nixosSystem { nixpkgs.lib.nixosSystem {
modules = modules =
[defaultNixosModule] [defaultNixosModule]
++ [ ++ [
( (rootPath + "/nixos/configurations/${hostName}")
let
dirPath = nixosConfigDir + "/${hostName}";
filePath = nixosConfigDir + "/${hostName}.nix";
in
if builtins.pathExists dirPath
then dirPath
else filePath
)
]; ];
specialArgs = { specialArgs = {
inherit inputs outputs rootPath; inherit inputs outputs rootPath;
}; };
}; };
in in
nixosConfigDir [
|> builtins.readDir "Tytonidae"
|> builtins.attrNames "Cape"
|> map (f: lib.removeSuffix ".nix" f) "Akun"
|> map (name: { ]
inherit name; |> (
value = makeNixConfiguration name; with lib;
}) flip genAttrs makeNixConfiguration
|> builtins.listToAttrs; );
}; };
} }

17
flake/overlays.nix Normal file
View file

@ -0,0 +1,17 @@
{
self,
inputs,
lib,
...
}: let
rootPath = ./..;
inherit (self) outputs;
importWithArgs = lib.flip import {inherit inputs outputs;};
in {
flake.overlays =
[
"modifications"
"additions"
]
|> (with lib; flip genAttrs (name: importWithArgs (rootPath + "/overlays/${name}")));
}

35
flake/perSystem.nix Normal file
View file

@ -0,0 +1,35 @@
{inputs, ...}: let
rootPath = ./..;
in {
imports = [
(rootPath + "/treefmt.nix")
];
perSystem = {
pkgs,
system,
lib,
self',
...
}: let
inherit (inputs) nixpkgs;
callPackages = lib.callPackagesWith (pkgs // {inherit callPackages inputs rootPath;});
in {
_module.args.pkgs = import nixpkgs {
inherit system;
config = {
allowUnfree = true;
};
};
devShells.default = pkgs.mkShell {
packages = with pkgs; [
nixd
typos
typos-lsp
just
nvfetcher
];
};
packages = callPackages (rootPath + "/pkgs") {};
checks = self'.packages;
};
}

View file

@ -1,8 +1,10 @@
{rootPath, ...}: { {
flake-parts-lib, flake-parts-lib,
lib, lib,
... ...
}: { }: let
rootPath = ./..;
in {
options = { options = {
flake = flake-parts-lib.mkSubmoduleOptions { flake = flake-parts-lib.mkSubmoduleOptions {
templates = lib.mkOption { templates = lib.mkOption {

View file

@ -1,11 +1,13 @@
{ {
pkgs,
inputs, inputs,
rootPath, rootPath,
callPackages,
lib,
pkgs,
... ...
}: let }: let
srcs = pkgs.callPackage ./_sources/generated.nix {}; srcs = callPackages ./_sources/generated.nix {};
callPackage = pkgs.lib.callPackageWith (pkgs // {inherit inputs srcs callPackage rootPath;}); callPackage = lib.callPackageWith (pkgs // {inherit inputs srcs callPackage rootPath;});
in in
{ {
pinentry-selector = callPackage ./pinentry-selector.nix {}; pinentry-selector = callPackage ./pinentry-selector.nix {};
@ -28,5 +30,5 @@ in
let let
firefox-addons = callPackage "${inputs.nur-rycee}/pkgs/firefox-addons/default.nix" {}; firefox-addons = callPackage "${inputs.nur-rycee}/pkgs/firefox-addons/default.nix" {};
in in
pkgs.lib.genAttrs ["immersive-translate" "tridactyl"] (name: firefox-addons."${name}") lib.genAttrs ["immersive-translate" "tridactyl"] (name: firefox-addons."${name}")
) )

60
treefmt.nix Normal file
View file

@ -0,0 +1,60 @@
{
perSystem = {pkgs, ...}: {
treefmt = {
programs = {
alejandra = {
enable = true;
excludes = ["pkgs/_sources/*.nix"];
};
biome = {
enable = true;
includes = ["*.json"];
excludes = ["pkgs/_sources/*.json"];
settings = {
javascript.formatter.enabled = false;
css.formatter.enabled = false;
};
};
dprint = {
enable = true;
includes = ["*.md" "*.toml" "*.yaml"];
excludes = ["secrets/*.yaml"];
settings = {
plugins = pkgs.dprint-plugins.getPluginList (plugins:
with plugins; [
dprint-plugin-toml
dprint-plugin-markdown
g-plane-pretty_yaml
]);
};
};
just = {
enable = true;
includes = [".justfile"];
};
typos = let
config = ./.typos.toml |> builtins.readFile |> builtins.fromTOML;
in {
enable = true;
includes = ["*"];
excludes = ["assets/*"] ++ config.files.extend-exclude;
configFile = toString ./.typos.toml;
# Disable all extra option in treefmt module.
# Use config file.
sort = false;
isolated = false;
hidden = false;
noIgnore = false;
noIgnoreDot = false;
noIgnoreGlobal = false;
noIgnoreParent = false;
noIgnoreVCS = false;
binary = false;
noCheckFilenames = false;
noCheckFiles = false;
noUnicode = false;
};
};
};
};
}