From 882c04a6bfe786be81f7cf909e40c74b429492a5 Mon Sep 17 00:00:00 2001 From: marc Date: Wed, 16 Nov 2022 20:28:32 +0100 Subject: [PATCH] Partial way to unifying nixos and home-manager modules --- flake.nix | 57 +++++++++++++----------- lib/attrs.nix | 2 + lib/modules.nix | 12 +++++ modules/options.nix | 71 ++++++++++++++++++++++++++++++ system/hosts/kopavogur/default.nix | 6 +-- system/hosts/reykjavik/default.nix | 2 +- system/modules/default.nix | 1 - system/modules/user.nix | 36 --------------- 8 files changed, 121 insertions(+), 66 deletions(-) create mode 100644 modules/options.nix delete mode 100644 system/modules/user.nix diff --git a/flake.nix b/flake.nix index a129f64..c13ca68 100644 --- a/flake.nix +++ b/flake.nix @@ -29,17 +29,24 @@ reykjavik = lib.nixosSystem { inherit system; inherit pkgs; - specialArgs = { inherit inputs; }; + specialArgs = { inherit lib inputs system; }; modules = [ ./system/modules ./system/profiles ./system/hosts/reykjavik - home-manager.nixosModules.home-manager { - home-manager.extraSpecialArgs = { inherit inputs; }; - home-manager.useGlobalPkgs = true; - home-manager.useUserPackages = true; + { + imports = + [ inputs.home-manager.nixosModules.home-manager ] + # All my personal modules + ++ (lib.my.mapModulesRec' (toString ./modules) import); + } + + { + # home-manager.extraSpecialArgs = { inherit inputs; }; + # home-manager.useGlobalPkgs = true; + # home-manager.useUserPackages = true; home-manager.users.marc = { imports = [ ./home/modules @@ -53,29 +60,29 @@ }; # - Kopavogur ---------------------------- - kopavogur = lib.nixosSystem { - inherit system; - inherit pkgs; - modules = [ + # kopavogur = lib.nixosSystem { + # inherit system; + # inherit pkgs; + # modules = [ - ./system/modules - ./system/profiles - ./system/hosts/kopavogur + # ./system/modules + # ./system/profiles + # ./system/hosts/kopavogur - home-manager.nixosModules.home-manager { - home-manager.useGlobalPkgs = true; - home-manager.useUserPackages = true; - home-manager.users.marc = { - imports = [ - ./home/modules - ./home/profiles - ./home/hosts/kopavogur.nix - ]; - }; - } + # home-manager.nixosModules.home-manager { + # home-manager.useGlobalPkgs = true; + # home-manager.useUserPackages = true; + # home-manager.users.marc = { + # imports = [ + # ./home/modules + # ./home/profiles + # ./home/hosts/kopavogur.nix + # ]; + # }; + # } - ]; - }; + # ]; + # }; }; }; diff --git a/lib/attrs.nix b/lib/attrs.nix index 0f8ebd1..05e8feb 100644 --- a/lib/attrs.nix +++ b/lib/attrs.nix @@ -11,6 +11,8 @@ rec { # (name -> value -> bool) # (name -> value -> { name = any; value = any; }) # attrs + # Generate an attribute set by mapping a function (f) to it and then filter + # by the predicate (pred) mapFilterAttrs = pred: f: attrs: filterAttrs pred (mapAttrs' f attrs); # Generate an attribute set by mapping a function over a list of values. diff --git a/lib/modules.nix b/lib/modules.nix index 47743f1..66bdfe8 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -6,6 +6,10 @@ let inherit (self.attrs) mapFilterAttrs; in rec { + # Returns an attribute set with the keys being the files in a directory + # and the values a function applied to their paths + # Only *.nix files and directories with a default.nix file are considered. + # The ".nix" suffix is removed from the keys of the attribute set. mapModules = dir: fn: mapFilterAttrs (n: v: @@ -22,9 +26,14 @@ rec { else nameValuePair "" null) (readDir dir); + # Like mapModules above, but it just returns the values + # (map the function fn over the files in a directory) mapModules' = dir: fn: attrValues (mapModules dir fn); + # Like mapModules, but recursive. That is, if there is a directory, the + # value will be another attribute set with its contents as keys and the funcion + # applied to them as values. mapModulesRec = dir: fn: mapFilterAttrs (n: v: @@ -39,6 +48,9 @@ rec { else nameValuePair "" null) (readDir dir); + # Like mapModulesRec but flattened and only the values. That is, it returns a list + # with fn applied to all *.nix files within the directory and subdirectories + # excluding default.nix files. mapModulesRec' = dir: fn: let dirs = diff --git a/modules/options.nix b/modules/options.nix new file mode 100644 index 0000000..fbd921f --- /dev/null +++ b/modules/options.nix @@ -0,0 +1,71 @@ +{ config, options, lib, home-manager, ... }: + +with lib; +with lib.my; +{ + options = with types; { + user = mkOpt attrs {}; + + home = { + file = mkOpt' attrs {} "Files to place directly in $HOME"; + configFile = mkOpt' attrs {} "Files to place in $XDG_CONFIG_HOME"; + dataFile = mkOpt' attrs {} "Files to place in $XDG_DATA_HOME"; + }; + + env = mkOption { + # env = { PATH = ["$PATH" ./test]; TEST = "test"; } + type = attrsOf (oneOf [ str path (listOf (either str path)) ]); + # env = { PATH = "$PATH:./test"; TEST = "test"; } + apply = mapAttrs + (n: v: if isList v + then concatMapStringsSep ":" (x: toString x) v + else (toString v)); + default = {}; + description = "Set environment variables that will be joined by a colon"; + }; + }; + + config = { + user = let name = "marc"; in { + inherit name; + description = "The primary user account"; + extraGroups = [ "wheel" ]; + isNormalUser = true; + home = "/home/${name}"; + group = "users"; + uid = 1000; + }; + + home-manager = { + # extraSpecialArgs = { inherit inputs; }; + useGlobalPkgs = true; + useUserPackages = true; + + users.${config.user.name} = { + home = { + file = mkAliasDefinitions options.home.file; + # Necessary for home-manager to work with flakes, otherwise it will + # look for a nixpkgs channel. + stateVersion = config.system.stateVersion; + }; + xdg = { + configFile = mkAliasDefinitions options.home.configFile; + dataFile = mkAliasDefinitions options.home.dataFile; + }; + }; + }; + + users.users.${config.user.name} = mkAliasDefinitions options.user; + + nix.settings = let users = [ "root" config.user.name ]; in { + trusted-users = users; + allowed-users = users; + }; + + env.PATH = [ "$PATH" ]; + + environment.extraInit = + concatStringsSep "\n" + (mapAttrsToList (n: v: "export ${n}=\"${v}\"") config.env); + }; +} diff --git a/system/hosts/kopavogur/default.nix b/system/hosts/kopavogur/default.nix index 960edd5..af17bc4 100644 --- a/system/hosts/kopavogur/default.nix +++ b/system/hosts/kopavogur/default.nix @@ -9,9 +9,9 @@ networking.hostName = "kopavogur"; - samfelag.modules.user = { - name = "marc"; - }; + # samfelag.modules.user = { + # name = "marc"; + # }; # - Bootloader --------------------------------- diff --git a/system/hosts/reykjavik/default.nix b/system/hosts/reykjavik/default.nix index f92f253..2c6d054 100644 --- a/system/hosts/reykjavik/default.nix +++ b/system/hosts/reykjavik/default.nix @@ -15,7 +15,7 @@ # - Modules ------------------------------------ samfelag.modules = { - user.name = "marc"; + # user.name = "marc"; bluetooth.enable = true; editors.emacs.enable = true; }; diff --git a/system/modules/default.nix b/system/modules/default.nix index e00008f..867c4ff 100644 --- a/system/modules/default.nix +++ b/system/modules/default.nix @@ -5,6 +5,5 @@ ./editors ./bluetooth.nix ./tailscale.nix - ./user.nix ]; } diff --git a/system/modules/user.nix b/system/modules/user.nix deleted file mode 100644 index 51e638d..0000000 --- a/system/modules/user.nix +++ /dev/null @@ -1,36 +0,0 @@ -{ config, lib, pkgs, self, ... }: - -let - cfg = config.samfelag.modules.user; -in -{ - options.samfelag.modules.user = { - name = lib.mkOption { - type = lib.types.str; - default = "marc"; - description = '' - Specifies the user name - ''; - }; - }; - - config = lib.mkMerge [ - { - programs.zsh.enable = true; - - users = { - defaultUserShell = pkgs.zsh; - users."${cfg.name}" = with cfg; { - description = "Marc Sastre Rienitz"; - isNormalUser = true; - extraGroups = [ "networkmanager" "wheel" ]; - }; - - # Do not allow users to be added or modified except through Nix configuration. - # mutableUsers = false; - }; - - nix.settings.trusted-users = [ "${cfg.name}" ]; - } - ]; -}