From 7ad9a62f2c60419a721ac42c45e7ede344714fe0 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Sat, 9 Jul 2022 13:54:05 +0800 Subject: [PATCH] profiles/users: move as a library function --- flake.nix | 6 +++ hosts/ni/default.nix | 19 +++---- lib/private.nix | 15 ++++++ modules/nixos/profiles/users.nix | 87 -------------------------------- 4 files changed, 31 insertions(+), 96 deletions(-) delete mode 100644 modules/nixos/profiles/users.nix diff --git a/flake.nix b/flake.nix index 050b1a39..2fd1d052 100644 --- a/flake.nix +++ b/flake.nix @@ -107,6 +107,12 @@ # The default configuration for our NixOS systems. hostDefaultConfig = { pkgs, system, ... }: { + # Only use imports as minimally as possible with the absolute + # requirements of a host. + imports = [ + inputs.home-manager.nixosModules.home-manager + ]; + # Bleeding edge, baybee! nix.package = pkgs.nixUnstable; diff --git a/hosts/ni/default.nix b/hosts/ni/default.nix index 637eb8e3..b8c1abff 100644 --- a/hosts/ni/default.nix +++ b/hosts/ni/default.nix @@ -1,4 +1,4 @@ -{ config, pkgs, inputs, ... }: +{ config, pkgs, lib, inputs, ... }: { imports = [ @@ -6,6 +6,15 @@ ./hardware-configuration.nix inputs.guix-overlay.nixosModules.guix-binary + + (lib.mapHomeManagerUser "foo-dogsquared" { + extraGroups = [ "wheel" "audio" "docker" "podman" "networkmanager" ]; + hashedPassword = + "$6$.cMYto0K0CHbpIMT$dRqyKs4q1ppzmTpdzy5FWP/V832a6X..FwM8CJ30ivK0nfLjQ7DubctxOZbeOtygfjcUd1PZ0nQoQpOg/WMvg."; + isNormalUser = true; + createHome = true; + home = "/home/foo-dogsquared"; + }) ]; boot.binfmt.emulatedSystems = [ @@ -31,14 +40,6 @@ virtualization.enable = true; neovim.enable = true; }; - users.users.foo-dogsquared.settings = { - extraGroups = [ "wheel" "audio" "docker" "podman" "networkmanager" ]; - hashedPassword = - "$6$.cMYto0K0CHbpIMT$dRqyKs4q1ppzmTpdzy5FWP/V832a6X..FwM8CJ30ivK0nfLjQ7DubctxOZbeOtygfjcUd1PZ0nQoQpOg/WMvg."; - isNormalUser = true; - createHome = true; - home = "/home/foo-dogsquared"; - }; }; tasks = { diff --git a/lib/private.nix b/lib/private.nix index 85f28159..ad2cbdb6 100644 --- a/lib/private.nix +++ b/lib/private.nix @@ -3,6 +3,21 @@ { lib }: rec { + mapHomeManagerUser = user: settings: + let + defaultUserConfig = { + extraGroups = [ "wheel" ]; + createHome = true; + home = "/home/${user}"; + }; + # TODO: Effectively override the option. + # We assume all users set with this module are normal users. + absoluteOverrides = { isNormalUser = true; }; + in { + home-manager.users."${user}" = import (lib.getUser "home-manager" user); + users.users."${user}" = defaultUserConfig // settings // absoluteOverrides; + }; + getSecret = path: ../secrets/${path}; getUsers = type: users: diff --git a/modules/nixos/profiles/users.nix b/modules/nixos/profiles/users.nix deleted file mode 100644 index dd6a22e6..00000000 --- a/modules/nixos/profiles/users.nix +++ /dev/null @@ -1,87 +0,0 @@ -# This enables home-manager specific configs and an easier modularization for -# user-specific configurations. This is specifically for creating a convenient -# way to create users from `users/home-manager`. -# -# If you're looking to create users from `users/nixos`, you can just import -# them directly. -{ inputs, config, options, lib, ... }: - -let - cfg = config.profiles.users; - users = lib.attrNames cfg.users; - homeManagerUserModules = lib.getUsers "home-manager" users; - homeManagerModules = lib.filesToAttr ../../home-manager; - - homeManagerUsers = lib.attrNames homeManagerUserModules; - nonexistentUsers = lib.filter (name: !lib.elem name homeManagerUsers) users; - - userOption = { name, config, ... }: { - options = { - settings = lib.mkOption { - type = lib.types.attrs; - description = '' - Configuration to be merged in users.users. - from NixOS configuration. - ''; - default = { }; - example = { - uid = 1234; - description = "John Doe"; - extraGroups = [ "wheel" "adbusers" "audio" ]; - }; - }; - }; - }; - - mapUsers = f: lib.mapAttrs f cfg.users; -in { - options.profiles.users = { - users = lib.mkOption { - default = { }; - description = '' - A set of users from the ./users/home-manager - directory to be included in the NixOS config. This will also create the - appropriate user settings in users.users in the - NixOS configuration. - ''; - example = { - foo-dogsquared.settings = { - extraGroups = [ "wheel" "audio" "libvirtd" ]; - }; - alice = { }; - bob = { }; - }; - type = with lib.types; attrsOf (submodule userOption); - }; - }; - - imports = [ inputs.home-manager.nixosModules.home-manager ]; - - config = { - assertions = [{ - assertion = (builtins.length nonexistentUsers) < 1; - message = "${ - lib.concatMapStringsSep ", " (u: "'${u}'") nonexistentUsers - } is not found in the `./users/home-manager` directory."; - }]; - - # Mapping each users to the respective user configuration. - # Setting users for home-manager. - home-manager.users = mapUsers (user: _: - let homeManagerUserModulePath = lib.getAttr user homeManagerUserModules; - in import homeManagerUserModulePath); - - # NixOS users. - users.users = mapUsers (user: opts: - let - defaultUserConfig = { - extraGroups = [ "wheel" ]; - createHome = true; - home = "/home/${user}"; - }; - # TODO: Effectively override the option. - # We assume all users set with this module are normal users. - absoluteOverrides = { isNormalUser = true; }; - in defaultUserConfig // opts.settings // absoluteOverrides); - }; -}