From db81831dc0edd2760cc35c5f4a3d2162d412e812 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Sun, 7 Jan 2024 15:14:21 -0300 Subject: [PATCH] feat: systems/set-user Small "helper" for handling users --- hosts/desktop/configuration.nix | 28 ++++------- hosts/desktop/home.nix | 24 +--------- modules/nixos/systems/set-user.nix | 74 ++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 41 deletions(-) create mode 100644 modules/nixos/systems/set-user.nix diff --git a/hosts/desktop/configuration.nix b/hosts/desktop/configuration.nix index 21f9b65..eed4912 100644 --- a/hosts/desktop/configuration.nix +++ b/hosts/desktop/configuration.nix @@ -7,6 +7,7 @@ { imports = [ ../../modules/nixos/config/host.nix + ../../modules/nixos/systems/set-user.nix # Include the results of the hardware scan. ./hardware-configuration.nix ]; @@ -26,27 +27,17 @@ programs.zsh.enable = true; # Define a user account. Don't forget to set a password with ‘passwd’. - users.users."guz" = { - isNormalUser = true; - description = "Guz"; - extraGroups = [ "networkmanager" "wheel" ]; - packages = with pkgs; [ - firefox - # thunderbird - ]; - shell = pkgs.zsh; - }; - - home-manager = { - extraSpecialArgs = { inherit inputs; }; - users = { - "guz" = import ./home.nix; - }; - }; + set-user.users = [ + { + username = "guz"; + shell = pkgs.zsh; + home = import ./home.nix; + } + ]; services.flatpak.enable = true; - environment.pathsToLink = [ "/share/zsh" ]; + environment.pathsToLink = [ " /share/zsh " ]; # Some programs need SUID wrappers, can be configured further or are # started in user sessions. @@ -69,3 +60,4 @@ } + diff --git a/hosts/desktop/home.nix b/hosts/desktop/home.nix index 26c1154..15ee48f 100644 --- a/hosts/desktop/home.nix +++ b/hosts/desktop/home.nix @@ -2,31 +2,11 @@ { imports = [ - # inputs.flatpaks.homeManagerModules.default - inputs.nix-flatpak.homeManagerModules.nix-flatpak ../../modules/home-manager/theme.nix ../../modules/home-manager/config/terminal.nix ]; # theme.accent = "f38ba8"; - # Home Manager needs a bit of information about you and the paths it should - # manage. - home.username = "guz"; - home.homeDirectory = "/home/guz"; - - - # This value determines the Home Manager release that your configuration is - # compatible with. This helps avoid breakage when a new Home Manager release - # introduces backwards incompatible changes. - # - # You should not change this value, even if you update Home Manager. If you do - # want to update the value, then make sure to first check the Home Manager - # release notes. - home.stateVersion = "23.11"; # Please read the comment before changing. - - # The home.packages option allows you to install Nix packages into your - # environment. - programs.bash = { enable = true; @@ -46,6 +26,7 @@ ]; home.packages = with pkgs; [ obsidian + firefox # # It is sometimes useful to fine-tune packages, for example, by applying # # overrides. You can do that directly here, just don't forget the # # parentheses. Maybe you want to install Nerd Fonts with a limited number of @@ -94,7 +75,4 @@ home.sessionVariables = { # EDITOR = "emacs"; }; - - # Let Home Manager install and manage itself. - programs.home-manager.enable = true; } diff --git a/modules/nixos/systems/set-user.nix b/modules/nixos/systems/set-user.nix new file mode 100644 index 0000000..8e8def4 --- /dev/null +++ b/modules/nixos/systems/set-user.nix @@ -0,0 +1,74 @@ +{ config, pkgs, inputs, lib, ... }: + +let + cfg = config.set-user; +in +{ + options.set-user = { + users = lib.mkOption { + default = [ ]; + # TODO: Fix types + /* type = lib.types.listOf { + username = lib.types.str; + description = lib.types.str; + normalUser = lib.types.bool; + shell = lib.types.package; + packages = lib.types.pkgs; + extraGroups = lib.types.listOf lib.types.str; + home = lib.types.anything; + flatpak = lib.types.bool; + }; */ + }; + }; + config = + let + home-default = user: { + imports = [ + ( + if user?flatpak && !user.flatpak + then null + else inputs.nix-flatpak.homeManagerModules.nix-flatpak + ) + ]; + programs.home-manager.enable = true; + home.username = user.username; + home.homeDirectory = "/home/${user.username}"; + home.stateVersion = "23.11"; # Do not change + }; + in + { + users.users = (builtins.listToAttrs + (map + (u: { + name = u.username; + value = { + description = + if u?description then u.description else u.username; + isNormalUser = + if u?normalUser then u.normalUser else true; + shell = + if u?shell then u.shell else pkgs.bash; + packages = + if u?packages then u.packages else [ ]; + extraGroups = + if u?extraGroups then u.extraGroups else [ "networkmanager" "wheel" ]; + }; + }) + cfg.users + ) + ); + home-manager.extraSpecialArgs = { inherit inputs; }; + home-manager.users = (builtins.listToAttrs + (map + (u: { + name = u.username; + value = + if u?home then lib.mkMerge [ (home-default u) u.home ] + else (home-default u); + }) + cfg.users + ) + ); + }; +} +