diff --git a/flake.nix b/flake.nix index 14e11f1..2752654 100644 --- a/flake.nix +++ b/flake.nix @@ -28,8 +28,17 @@ outputs = inputs@{ self, utils, nixpkgs, nixos-hardware, nur, home-manager , emacs-overlay, ... }: - let inherit (utils.lib) mkFlake exportModules; + let + inherit (utils.lib) mkFlake; + inherit (self.lib.my) mapModules mapModulesRec'; in mkFlake { + lib = nixpkgs.lib.extend (self: super: { + my = import ./lib { + inherit inputs; + pkgs = nixpkgs; + lib = self; + }; + }); inherit self inputs; supportedSystems = [ "x86_64-linux" ]; @@ -48,7 +57,7 @@ sharedOverlays = [ nur.overlay emacs-overlay.overlay ]; hostDefaults = { - modules = [ + modules = mapModulesRec' ./modules import ++ [ home-manager.nixosModules.home-manager { home-manager.useGlobalPkgs = true; diff --git a/lib/attrs.nix b/lib/attrs.nix new file mode 100644 index 0000000..0f8ebd1 --- /dev/null +++ b/lib/attrs.nix @@ -0,0 +1,26 @@ +{ lib, ... }: + +with builtins; +with lib; +rec { + # attrsToList + attrsToList = attrs: + mapAttrsToList (name: value: { inherit name value; }) attrs; + + # mapFilterAttrs :: + # (name -> value -> bool) + # (name -> value -> { name = any; value = any; }) + # attrs + mapFilterAttrs = pred: f: attrs: filterAttrs pred (mapAttrs' f attrs); + + # Generate an attribute set by mapping a function over a list of values. + genAttrs' = values: f: listToAttrs (map f values); + + # anyAttrs :: (name -> value -> bool) attrs + anyAttrs = pred: attrs: + any (attr: pred attr.name attr.value) (attrsToList attrs); + + # countAttrs :: (name -> value -> bool) attrs + countAttrs = pred: attrs: + count (attr: pred attr.name attr.value) (attrsToList attrs); +} diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..2d19616 --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,19 @@ +{ inputs, lib, pkgs, ... }: + +let + inherit (lib) makeExtensible attrValues foldr; + inherit (modules) mapModules; + + modules = import ./modules.nix { + inherit lib; + self.attrs = import ./attrs.nix { + inherit lib; + self = { }; + }; + }; + + mylib = makeExtensible (self: + with self; + mapModules ./. (file: import file { inherit self lib pkgs inputs; })); + +in mylib.extend (self: super: foldr (a: b: a // b) { } (attrValues super)) diff --git a/lib/modules.nix b/lib/modules.nix new file mode 100644 index 0000000..55794bf --- /dev/null +++ b/lib/modules.nix @@ -0,0 +1,40 @@ +{ self, lib, ... }: + +let + inherit (builtins) attrValues readDir pathExists concatLists; + inherit (lib) + id mapAttrsToList filterAttrs hasPrefix hasSuffix nameValuePair + removeSuffix; + inherit (self.attrs) mapFilterAttrs; +in rec { + mapModules = dir: fn: + mapFilterAttrs (n: v: v != null && !(hasPrefix "_" n)) (n: v: + let path = "${toString dir}/${n}"; + in if v == "directory" && pathExists "${path}/default.nix" then + nameValuePair n (fn path) + else if v == "regular" && n != "default.nix" && hasSuffix ".nix" n then + nameValuePair (removeSuffix ".nix" n) (fn path) + else + nameValuePair "" null) (readDir dir); + + mapModules' = dir: fn: attrValues (mapModules dir fn); + + mapModulesRec = dir: fn: + mapFilterAttrs (n: v: v != null && !(hasPrefix "_" n)) (n: v: + let path = "${toString dir}/${n}"; + in if v == "directory" then + nameValuePair n (mapModulesRec path fn) + else if v == "regular" && n != "default.nix" && hasSuffix ".nix" n then + nameValuePair (removeSuffix ".nix" n) (fn path) + else + nameValuePair "" null) (readDir dir); + + mapModulesRec' = dir: fn: + let + dirs = mapAttrsToList (k: _: "${dir}/${k}") + (filterAttrs (n: v: v == "directory" && !(hasPrefix "_" n)) + (readDir dir)); + files = attrValues (mapModules dir id); + paths = files ++ concatLists (map (d: mapModulesRec' d id) dirs); + in map fn paths; +} diff --git a/lib/nixos.nix b/lib/nixos.nix new file mode 100644 index 0000000..4595af7 --- /dev/null +++ b/lib/nixos.nix @@ -0,0 +1,25 @@ +{ inputs, lib, pkgs, ... }: + +with lib; +with lib.my; +let sys = "x86_64-linux"; +in { + mkHost = path: attrs @ { system ? sys, ... }: + nixosSystem { + inherit system; + specialArgs = { inherit lib inputs system; }; + modules = [ + { + nixpkgs.pkgs = pkgs; + networking.hostName = mkDefault (removeSuffix ".nix" (baseNameOf path)); + } + (filterAttrs (n: v: !elem n [ "system" ]) attrs) + ../. # /default.nix + (import path) + ]; + }; + + mapHosts = dir: attrs @ { system ? system, ... }: + mapModules dir + (hostPath: mkHost hostPath attrs); +} diff --git a/lib/options.nix b/lib/options.nix new file mode 100644 index 0000000..f659189 --- /dev/null +++ b/lib/options.nix @@ -0,0 +1,16 @@ +{ lib, ... }: + +let inherit (lib) mkOption types; +in rec { + mkOpt = type: default: mkOption { inherit type default; }; + + mkOpt' = type: default: description: + mkOption { inherit type default description; }; + + mkBoolOpt = default: + mkOption { + inherit default; + type = types.bool; + example = true; + }; +} diff --git a/modules/applications/default.nix b/modules/applications/default.nix deleted file mode 100644 index ab4cf35..0000000 --- a/modules/applications/default.nix +++ /dev/null @@ -1,20 +0,0 @@ -{ config, pkgs, ... }: { - imports = [ - ./1password.nix - ./blueman.nix - ./dunst.nix - ./emacs.nix - ./gammastep.nix - ./gnome-common.nix - ./kdeconnect.nix - ./kitty.nix - ./media.nix - ./nextcloud.nix - ./spotify.nix - ./zathura.nix - ./zeal.nix - - ./i3 - ./firefox - ]; -} diff --git a/modules/applications/firefox/default.nix b/modules/applications/firefox/default.nix index 661ae7f..5f49e1d 100644 --- a/modules/applications/firefox/default.nix +++ b/modules/applications/firefox/default.nix @@ -3,7 +3,6 @@ let cfg = config.this.application.firefox; graphical = config.this.graphical; in with lib; { - imports = [ ./tridactyl.nix ]; options.this.application.firefox = { enable = mkOption { default = graphical.enable; diff --git a/modules/applications/i3/default.nix b/modules/applications/i3/default.nix deleted file mode 100644 index 21a0f8f..0000000 --- a/modules/applications/i3/default.nix +++ /dev/null @@ -1,3 +0,0 @@ -{ config, pkgs, ... }: { - imports = [ ./i3.nix ./picom.nix ./polybar.nix ./rofi.nix ]; -} diff --git a/modules/default.nix b/modules/default.nix deleted file mode 100644 index cdc2698..0000000 --- a/modules/default.nix +++ /dev/null @@ -1,11 +0,0 @@ -{ config, pkgs, lib, ... }: -with lib; -let -in { - - options.this.graphical = { - enable = mkEnableOption "Does this actually need X/Wayland"; - }; - - imports = [ ./applications ./lang ./system ]; -} diff --git a/modules/lang/default.nix b/modules/lang/default.nix deleted file mode 100644 index e460ec3..0000000 --- a/modules/lang/default.nix +++ /dev/null @@ -1,3 +0,0 @@ -{ config, pkgs, ... }: { - imports = [ ./golang.nix ./javascript.nix ./bash.nix ]; -} diff --git a/modules/options.nix b/modules/options.nix new file mode 100644 index 0000000..629f16a --- /dev/null +++ b/modules/options.nix @@ -0,0 +1,8 @@ +{ config, pkgs, lib, ... }: { + + options.this.graphical = { + enable = lib.mkEnableOption "Does this actually need X/Wayland"; + }; + + # imports = [ ./applications ./lang ./system ]; +} diff --git a/modules/system/default.nix b/modules/system/default.nix deleted file mode 100644 index a772a5d..0000000 --- a/modules/system/default.nix +++ /dev/null @@ -1,15 +0,0 @@ -{ config, pkgs, ... }: { - imports = [ - ./boot.nix - ./cli.nix - ./displaymanager.nix - ./flatpak.nix - ./fonts.nix - ./gtk.nix - ./keyring.nix - ./power.nix - ./xdg.nix - ./yubikey.nix - ./zfs.nix - ]; -} diff --git a/modules/system/displaymanager.nix b/modules/system/displaymanager.nix index f9464a7..56f5a97 100644 --- a/modules/system/displaymanager.nix +++ b/modules/system/displaymanager.nix @@ -1,10 +1,10 @@ { config, lib, pkgs, ... }: let - cfg = config.system.displaymanager; + cfg = config.this.system.displaymanager; graphical = config.this.graphical; in with lib; { options = { - system.displaymanager.enable = mkOption { + this.system.displaymanager.enable = mkOption { default = graphical.enable; type = with types; bool; }; diff --git a/profiles/minimal.nix b/profiles/minimal.nix index 3f96e1d..dcc38be 100644 --- a/profiles/minimal.nix +++ b/profiles/minimal.nix @@ -1,4 +1,4 @@ -{ config, pkgs, ... }: +{ inputs, config, pkgs, ... }: let publicKey = pkgs.fetchurl { @@ -8,8 +8,8 @@ let in { imports = [ - ../modules/. # This imports /modules/default.nix - ../modules/system/xdg.nix + #../modules/. # This imports /modules/default.nix + #../modules/system/xdg.nix ]; # Allow Cleanup, nix, & flakes nix = {