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";
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 = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
# nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small";
@ -140,130 +171,4 @@
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,
inputs,
flake-parts-lib,
self,
...
}: let
inherit (self) outputs;
inherit (inputs) deploy-rs;
mkDeployNode = {
hostName,
unixName ? "deploy",
@ -18,7 +21,7 @@
system = {
user = "root";
path =
inputs.deploy-rs.lib."${system}".activate.nixos
deploy-rs.lib."${system}".activate.nixos
outputs.nixosConfigurations."${hostName}";
};
};

View file

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

View file

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

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,
lib,
...
}: {
}: let
rootPath = ./..;
in {
options = {
flake = flake-parts-lib.mkSubmoduleOptions {
templates = lib.mkOption {

View file

@ -1,11 +1,13 @@
{
pkgs,
inputs,
rootPath,
callPackages,
lib,
pkgs,
...
}: let
srcs = pkgs.callPackage ./_sources/generated.nix {};
callPackage = pkgs.lib.callPackageWith (pkgs // {inherit inputs srcs callPackage rootPath;});
srcs = callPackages ./_sources/generated.nix {};
callPackage = lib.callPackageWith (pkgs // {inherit inputs srcs callPackage rootPath;});
in
{
pinentry-selector = callPackage ./pinentry-selector.nix {};
@ -28,5 +30,5 @@ in
let
firefox-addons = callPackage "${inputs.nur-rycee}/pkgs/firefox-addons/default.nix" {};
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;
};
};
};
};
}