From 2c997cddceb4f2d3c0735aff42e15a4129fe762a Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Fri, 11 Jul 2025 21:04:42 +0800 Subject: [PATCH] Add extra function for lib, and refactor with `loadImports` --- flake.nix | 32 +++++++++++------ flake/default.nix | 10 ------ flake/home.nix | 7 +++- flake/nixos.nix | 7 ++-- flake/overlays.nix | 2 +- flake/perSystem.nix | 10 +++--- flake/templates.nix | 5 ++- home/david/configurations/Akun/default.nix | 5 ++- .../configurations/Tytonidae/default.nix | 5 ++- home/david/modules/default.nix | 12 +++---- home/david/modules/programs/default.nix | 23 ++----------- home/extra/default.nix | 10 +++--- home/modules/default.nix | 12 +++---- home/modules/programs/default.nix | 22 ++---------- lib/default.nix | 17 ++++++++++ nixos/configurations/Akun/default.nix | 11 ++---- nixos/configurations/Cape/default.nix | 18 ++++------ nixos/configurations/Tytonidae/default.nix | 20 +++-------- .../Tytonidae/specialisation/default.nix | 3 ++ nixos/modules/containers/default.nix | 5 +-- nixos/modules/default.nix | 16 ++------- nixos/modules/gui/default.nix | 6 +--- nixos/modules/programs/caddy/default.nix | 6 +--- nixos/modules/programs/default.nix | 34 ++----------------- nixos/modules/programs/juicity/default.nix | 4 +-- nixos/modules/programs/nixvim/default.nix | 2 +- nixos/modules/programs/rustypaste/default.nix | 4 +-- nixos/modules/virtualisation/default.nix | 7 ++-- 28 files changed, 111 insertions(+), 204 deletions(-) delete mode 100644 flake/default.nix create mode 100644 lib/default.nix create mode 100644 nixos/configurations/Tytonidae/specialisation/default.nix diff --git a/flake.nix b/flake.nix index 8a2b233..9c575da 100644 --- a/flake.nix +++ b/flake.nix @@ -2,35 +2,45 @@ description = "A simple NixOS flakes"; outputs = { - nixpkgs, flake-parts, flake-utils, home-manager, treefmt-nix, + nixpkgs, ... - } @ inputs: - flake-parts.lib.mkFlake {inherit inputs;} { + } @ inputs: let + nixpkgs-lib = nixpkgs.lib; + lib = nixpkgs-lib.extend (import ./lib); + in + flake-parts.lib.mkFlake { + inherit inputs; + specialArgs = { + inherit lib; + rootPath = ./.; + }; + } ({lib, ...}: { systems = flake-utils.lib.defaultSystems; - imports = [ - home-manager.flakeModules.home-manager - treefmt-nix.flakeModule - - ./flake - ]; + imports = + [ + home-manager.flakeModules.home-manager + treefmt-nix.flakeModule + ] + ++ lib.youthlic.loadImports ./flake; flake = { + inherit lib; nix.settings = { # substituters shared in home-manager and nixos configuration substituters = let cachix = x: "https://${x}.cachix.org"; in - nixpkgs.lib.flatten [ + lib.flatten [ (cachix "nix-community") "https://cache.nixos.org" (cachix "cosmic") ]; }; }; - }; + }); inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; diff --git a/flake/default.nix b/flake/default.nix deleted file mode 100644 index b2c10cc..0000000 --- a/flake/default.nix +++ /dev/null @@ -1,10 +0,0 @@ -{ - imports = [ - ./nixos.nix - ./home.nix - ./templates.nix - ./perSystem.nix - ./deploy.nix - ./overlays.nix - ]; -} diff --git a/flake/home.nix b/flake/home.nix index 50e7a26..4937c9d 100644 --- a/flake/home.nix +++ b/flake/home.nix @@ -2,9 +2,9 @@ lib, inputs, self, + rootPath, ... }: let - rootPath = ./..; inherit (self) outputs; homeModules = ( @@ -46,6 +46,11 @@ ]) ++ [ homeModules."${unixName}" + ] + ++ [ + { + lib = {inherit (lib) youthlic;}; + } ]; extraSpecialArgs = { inherit diff --git a/flake/nixos.nix b/flake/nixos.nix index d38904f..2d6c41d 100644 --- a/flake/nixos.nix +++ b/flake/nixos.nix @@ -2,11 +2,10 @@ inputs, lib, self, + rootPath, ... }: let - rootPath = ./..; inherit (self) outputs; - inherit (inputs) nixpkgs; in { flake = { nixosModules = { @@ -15,10 +14,10 @@ in { }; nixosConfigurations = let makeNixosConfiguration = hostName: - nixpkgs.lib.nixosSystem { + lib.nixosSystem { modules = [(rootPath + "/nixos/configurations/${hostName}")]; specialArgs = { - inherit inputs outputs rootPath; + inherit inputs outputs rootPath lib; }; }; in diff --git a/flake/overlays.nix b/flake/overlays.nix index 05cedf1..c47dc4c 100644 --- a/flake/overlays.nix +++ b/flake/overlays.nix @@ -2,9 +2,9 @@ self, inputs, lib, + rootPath, ... }: let - rootPath = ./..; inherit (self) outputs; importWithArgs = lib.flip import {inherit inputs outputs;}; in { diff --git a/flake/perSystem.nix b/flake/perSystem.nix index 5119c78..b6898c6 100644 --- a/flake/perSystem.nix +++ b/flake/perSystem.nix @@ -1,6 +1,8 @@ -{inputs, ...}: let - rootPath = ./..; -in { +{ + inputs, + rootPath, + ... +}: { imports = [ (rootPath + "/treefmt.nix") ]; @@ -12,7 +14,7 @@ in { ... }: let inherit (inputs) nixpkgs; - callPackages = lib.callPackagesWith (pkgs // {inherit callPackages inputs rootPath;}); + callPackages = lib.callPackagesWith (pkgs // {inherit callPackages inputs rootPath lib;}); in { _module.args.pkgs = import nixpkgs { inherit system; diff --git a/flake/templates.nix b/flake/templates.nix index a1396f9..22b75a4 100644 --- a/flake/templates.nix +++ b/flake/templates.nix @@ -1,10 +1,9 @@ { flake-parts-lib, lib, + rootPath, ... -}: let - rootPath = ./..; -in { +}: { options = { flake = flake-parts-lib.mkSubmoduleOptions { templates = lib.mkOption { diff --git a/home/david/configurations/Akun/default.nix b/home/david/configurations/Akun/default.nix index c83ff28..9023533 100644 --- a/home/david/configurations/Akun/default.nix +++ b/home/david/configurations/Akun/default.nix @@ -1,12 +1,11 @@ { pkgs, + lib, unixName, config, ... }: { - imports = [ - ./niri.nix - ]; + imports = lib.youthlic.loadImports ./.; youthlic = { xdg-dirs.enable = true; programs = let diff --git a/home/david/configurations/Tytonidae/default.nix b/home/david/configurations/Tytonidae/default.nix index d643f5b..1fee784 100644 --- a/home/david/configurations/Tytonidae/default.nix +++ b/home/david/configurations/Tytonidae/default.nix @@ -1,12 +1,11 @@ { pkgs, + lib, config, unixName, ... }: { - imports = [ - ./niri.nix - ]; + imports = lib.youthlic.loadImports ./.; youthlic = { xdg-dirs.enable = true; programs = let diff --git a/home/david/modules/default.nix b/home/david/modules/default.nix index 26bb09d..903eb35 100644 --- a/home/david/modules/default.nix +++ b/home/david/modules/default.nix @@ -1,9 +1,9 @@ -{pkgs, ...}: { - imports = [ - ./wallpaper.nix - ./programs - ./emails.nix - ]; +{ + lib, + pkgs, + ... +}: { + imports = lib.youthlic.loadImports ./.; config = { youthlic.programs = { zoxide.enable = true; diff --git a/home/david/modules/programs/default.nix b/home/david/modules/programs/default.nix index 6fd6896..1f8bd9e 100644 --- a/home/david/modules/programs/default.nix +++ b/home/david/modules/programs/default.nix @@ -1,22 +1,3 @@ -{...}: { - imports = [ - ./openssh.nix - ./niri - ./wluma.nix - ./helix.nix - ./firefox.nix - ./waybar.nix - ./zed.nix - ./alacritty - ./ghostty.nix - ./fuzzel.nix - ./mpv.nix - ./swaylock.nix - ./thunderbird.nix - ./chromium.nix - ./foot - ./swaync.nix - ./espanso.nix - ./waypaper.nix - ]; +{lib, ...}: { + imports = lib.youthlic.loadImports ./.; } diff --git a/home/extra/default.nix b/home/extra/default.nix index 6b11455..bfc3e75 100644 --- a/home/extra/default.nix +++ b/home/extra/default.nix @@ -1,11 +1,13 @@ -{inputs, ...}: { +{ + lib, + inputs, + ... +}: { imports = (with inputs; [ niri-flake.homeModules.niri stylix.homeManagerModules.stylix chaotic.homeManagerModules.default ]) - ++ [ - ./nix.nix - ]; + ++ (lib.youthlic.loadImports ./.); } diff --git a/home/modules/default.nix b/home/modules/default.nix index 4ef350f..eb2da28 100644 --- a/home/modules/default.nix +++ b/home/modules/default.nix @@ -1,14 +1,14 @@ -{inputs, ...}: { +{ + lib, + inputs, + ... +}: { imports = (with inputs; [ sops-nix.homeManagerModules.sops betterfox-nix.homeManagerModules.betterfox ]) - ++ [ - ./programs - ./xdg-dirs.nix - ./i18n - ]; + ++ lib.youthlic.loadImports ./.; config = { programs.direnv = { diff --git a/home/modules/programs/default.nix b/home/modules/programs/default.nix index 85ccc78..1f8bd9e 100644 --- a/home/modules/programs/default.nix +++ b/home/modules/programs/default.nix @@ -1,21 +1,3 @@ -{...}: { - imports = [ - ./rustypaste-cli.nix - ./atuin.nix - ./bash.nix - ./fish.nix - ./kvm.nix - ./starship - ./gpg - ./sops.nix - ./git.nix - ./helix - ./jujutsu.nix - ./yazi.nix - ./zoxide.nix - ./fzf.nix - ./eza.nix - ./ion.nix - ./awscli.nix - ]; +{lib, ...}: { + imports = lib.youthlic.loadImports ./.; } diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..c62e543 --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,17 @@ +final: _prev: { + youthlic = { + loadImports' = dir: f: + if (builtins.pathExists dir && (builtins.readFileType dir) == "directory") + then + with final; + pipe dir [ + builtins.readDir + attrNames + (filter (name: name != "default.nix")) + f + (map (name: dir + "/${name}")) + ] + else []; + loadImports = with final; flip youthlic.loadImports' (x: x); + }; +} diff --git a/nixos/configurations/Akun/default.nix b/nixos/configurations/Akun/default.nix index 9099163..c9a895a 100644 --- a/nixos/configurations/Akun/default.nix +++ b/nixos/configurations/Akun/default.nix @@ -1,6 +1,7 @@ { inputs, pkgs, + lib, outputs, ... }: { @@ -11,15 +12,9 @@ common-pc-laptop-ssd ]) ++ [ - ./gui.nix - ./hardware-configuration.nix - ./stylix.nix - ./users - ./networking.nix - ./disk-config.nix - outputs.nixosModules.gui - ]; + ] + ++ (lib.youthlic.loadImports ./.); youthlic = { users.deploy.enable = true; diff --git a/nixos/configurations/Cape/default.nix b/nixos/configurations/Cape/default.nix index fea157b..467d67e 100644 --- a/nixos/configurations/Cape/default.nix +++ b/nixos/configurations/Cape/default.nix @@ -1,20 +1,14 @@ { pkgs, + lib, outputs, ... }: { - imports = [ - ./forgejo.nix - ./networking.nix - ./stylix.nix - ./hardware-configuration.nix - ./users - ./disko-config.nix - ./miniflux.nix - ./radicle.nix - - outputs.nixosModules.default - ]; + imports = + [ + outputs.nixosModules.default + ] + ++ (lib.youthlic.loadImports ./.); youthlic = { home-manager = { diff --git a/nixos/configurations/Tytonidae/default.nix b/nixos/configurations/Tytonidae/default.nix index 7f4b06f..af93c5b 100644 --- a/nixos/configurations/Tytonidae/default.nix +++ b/nixos/configurations/Tytonidae/default.nix @@ -1,5 +1,6 @@ { pkgs, + lib, inputs, outputs, ... @@ -13,21 +14,10 @@ common-pc-laptop-ssd asus-battery ]) - ++ [ - ./users - ./stylix.nix - ./gui.nix - - # Include the hardware related config - ./hardware-configuration.nix - ./networking.nix - ./disk-config.nix - ./hardware.nix - ./specialisation/kde.nix - ./specialisation/niri-hybrid.nix - - outputs.nixosModules.gui - ]; + ++ (with outputs; [ + nixosModules.gui + ]) + ++ (lib.youthlic.loadImports ./.); youthlic = { home-manager = { diff --git a/nixos/configurations/Tytonidae/specialisation/default.nix b/nixos/configurations/Tytonidae/specialisation/default.nix new file mode 100644 index 0000000..1f8bd9e --- /dev/null +++ b/nixos/configurations/Tytonidae/specialisation/default.nix @@ -0,0 +1,3 @@ +{lib, ...}: { + imports = lib.youthlic.loadImports ./.; +} diff --git a/nixos/modules/containers/default.nix b/nixos/modules/containers/default.nix index 7a4e588..e39e453 100644 --- a/nixos/modules/containers/default.nix +++ b/nixos/modules/containers/default.nix @@ -5,10 +5,7 @@ }: let cfg = config.youthlic.containers; in { - imports = [ - ./forgejo.nix - ./miniflux.nix - ]; + imports = lib.youthlic.loadImports ./.; options = { youthlic.containers = { enable = lib.mkEnableOption "containers"; diff --git a/nixos/modules/default.nix b/nixos/modules/default.nix index 37a9228..f70ea62 100644 --- a/nixos/modules/default.nix +++ b/nixos/modules/default.nix @@ -1,15 +1,3 @@ -{ - imports = [ - ./containers - ./deploy - ./nix.nix - ./home.nix - ./sops.nix - ./i18n.nix - ./gui - ./programs - ./hardware.nix - ./virtualisation - ./documentation.nix - ]; +{lib, ...}: { + imports = with lib; youthlic.loadImports' ./. (filter (name: name != "top-level")); } diff --git a/nixos/modules/gui/default.nix b/nixos/modules/gui/default.nix index f2904ab..1814e97 100644 --- a/nixos/modules/gui/default.nix +++ b/nixos/modules/gui/default.nix @@ -6,11 +6,7 @@ }: let cfg = config.youthlic.gui; in { - imports = [ - ./niri.nix - ./cosmic.nix - ./kde.nix - ]; + imports = with lib; youthlic.loadImports ./.; options = { youthlic.gui = { enabled = lib.mkOption { diff --git a/nixos/modules/programs/caddy/default.nix b/nixos/modules/programs/caddy/default.nix index efa3eb5..641cb33 100644 --- a/nixos/modules/programs/caddy/default.nix +++ b/nixos/modules/programs/caddy/default.nix @@ -5,11 +5,7 @@ }: let cfg = config.youthlic.programs.caddy; in { - imports = [ - ./radicle-explorer.nix - ./OuterWildsTextAdventure.nix - ./garage.nix - ]; + imports = lib.youthlic.loadImports ./.; options = { youthlic.programs.caddy = { enable = lib.mkEnableOption "caddy"; diff --git a/nixos/modules/programs/default.nix b/nixos/modules/programs/default.nix index a3b1f54..1f8bd9e 100644 --- a/nixos/modules/programs/default.nix +++ b/nixos/modules/programs/default.nix @@ -1,33 +1,3 @@ -{...}: { - imports = [ - ./transfer-sh.nix - ./rustypaste - ./mautrix-telegram.nix - ./caddy - ./dae - ./forgejo.nix - ./kanata.nix - ./nh.nix - ./open-webui.nix - ./openssh.nix - ./postgresql.nix - ./steam.nix - ./tailscale.nix - ./transmission.nix - ./nix-ld.nix - ./juicity - ./miniflux.nix - ./guix.nix - ./asusd.nix - ./owncast.nix - ./minio.nix - ./supergfxd.nix - ./radicle.nix - ./wshowkeys.nix - ./bash.nix - ./obs.nix - ./sudo-rs.nix - ./garage.nix - ./nixvim - ]; +{lib, ...}: { + imports = lib.youthlic.loadImports ./.; } diff --git a/nixos/modules/programs/juicity/default.nix b/nixos/modules/programs/juicity/default.nix index 2543c9e..eac4b24 100644 --- a/nixos/modules/programs/juicity/default.nix +++ b/nixos/modules/programs/juicity/default.nix @@ -6,9 +6,7 @@ }: let cfg = config.youthlic.programs.juicity; in { - imports = [ - ./template.nix - ]; + imports = lib.youthlic.loadImports ./.; options = { youthlic.programs.juicity = { client = { diff --git a/nixos/modules/programs/nixvim/default.nix b/nixos/modules/programs/nixvim/default.nix index ebb4299..a86ca40 100644 --- a/nixos/modules/programs/nixvim/default.nix +++ b/nixos/modules/programs/nixvim/default.nix @@ -5,7 +5,7 @@ }: let cfg = config.youthlic.programs.nixvim; in { - imports = [./option.nix]; + imports = lib.youthlic.loadImports ./.; options = { youthlic.programs.nixvim = { enable = lib.mkEnableOption "nixvim"; diff --git a/nixos/modules/programs/rustypaste/default.nix b/nixos/modules/programs/rustypaste/default.nix index f18fbfc..3165cfc 100644 --- a/nixos/modules/programs/rustypaste/default.nix +++ b/nixos/modules/programs/rustypaste/default.nix @@ -5,9 +5,7 @@ }: let cfg = config.youthlic.programs.rustypaste; in { - imports = [ - ./template.nix - ]; + imports = lib.youthlic.loadImports ./.; options = { youthlic.programs.rustypaste = { diff --git a/nixos/modules/virtualisation/default.nix b/nixos/modules/virtualisation/default.nix index 3d299fd..1f8bd9e 100644 --- a/nixos/modules/virtualisation/default.nix +++ b/nixos/modules/virtualisation/default.nix @@ -1,6 +1,3 @@ -{ - imports = [ - ./kvm.nix - ./waydroid.nix - ]; +{lib, ...}: { + imports = lib.youthlic.loadImports ./.; }