diff --git a/nix/homes/rafiq/git.nix b/nix/homes/rafiq/git.nix new file mode 100644 index 0000000..62bb79b --- /dev/null +++ b/nix/homes/rafiq/git.nix @@ -0,0 +1,22 @@ +{ + flake.homes.rafiq = { + home.shellAliases = { + gs = "git status"; + gc = "git commit"; + gcam = "git commit -am"; + gu = "git push"; + gy = "git pull"; + gdh = "git diff HEAD"; + }; + programs.git = { + signing.signByDefault = true; + extraConfig = { + init.defaultBranch = "prime"; + push.autoSetupRemote = true; + pull.rebase = false; + core.editor = "$EDITOR"; + gpg.format = "ssh"; + }; + }; + }; +} diff --git a/nix/lib/default.nix b/nix/lib/default.nix index b75aefc..0e94c00 100644 --- a/nix/lib/default.nix +++ b/nix/lib/default.nix @@ -5,7 +5,92 @@ let in { flake.lib = { + /** + Remove the top level attributes from an attribute set and return the merged attributes. + + # Inputs + + `attrset` + + : An attribute set to flatten. + + # Type + + ``` + flattenAttrs :: AttrSet -> AttrSet + ``` + + # Examples + :::{.example} + ## `flattenAttrs` usage example + + ```nix + flattenAttrs { x = { a = 1; b = 2; }; y = { c = 3; d = 4; }; } + => { a = 1; b = 2; c = 3; d = 4; } + ``` + */ flattenAttrs = attrset: concatMapAttrs (_: v: v) attrset; - forAllUsers = f: mapAttrs f cfg.manifest.users; + + /** + Return an attribute set for use with a option that needs to be used for all users. + + # Inputs + + `attrset` + + : An attribute set to apply to all the users. + + # Type + + ``` + forAllUsers :: AttrSet -> AttrSet + ``` + + # Examples + :::{.example} + ## `forAllUsers` usage example + + ```nix + flake.manifest.users.rafiq = { ... }; + flake.modules.nixos.default.users = forAllUsers { + isNormalUser = true; + }; + => flake.modules.nixos.default.users.rafiq.isNormalUser = true; + ``` + + ::: + */ + forAllUsers = attrset: mapAttrs (_: _: attrset) cfg.manifest.users; + + /** + Like forAllUsers, but passes in the name and value from the manifest. + + # Inputs + + `f` + + : A function that takes an attribute name and its value, and returns the new value for the attribute. + + # Type + + ``` + forAllUsers' :: (String -> Any -> Any) -> AttrSet + ``` + + # Examples + :::{.example} + ## `forAllUsers'` usage example + + ```nix + flake.manifest.users.rafiq = { ... }; + flake.modules.homeManager.users = forAllUsers' (name: value: { + home.username = name; + }); + => flake.modules.homeManager.default.users.rafiq.home.username = "rafiq"; + ``` + + ::: + */ + forAllUsers' = f: mapAttrs f cfg.manifest.users; }; } diff --git a/nix/manifest.nix b/nix/manifest.nix index f3cd69c..cd305e2 100644 --- a/nix/manifest.nix +++ b/nix/manifest.nix @@ -15,7 +15,7 @@ in flake.manifest = { users.rafiq = { primary = true; - username = "rafiq"; # If we don't set this here we have to do some weird shit + name = "Mohammad Rafiq"; email = "rafiq@rrv.sh"; shell = "fish"; pubkey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILdsZyY3gu8IGB8MzMnLdh+ClDxQQ2RYG9rkeetIKq8n rafiq"; diff --git a/nix/modules/git.nix b/nix/modules/git.nix new file mode 100644 index 0000000..aee5432 --- /dev/null +++ b/nix/modules/git.nix @@ -0,0 +1,12 @@ +{ + flake.modules.homeManager.default = + { manifest, config, ... }: + { + home.sessionVariables.GIT_CONFIG_GLOBAL = "$HOME/.config/git/config"; + programs.git = { + userName = manifest.users.${config.home.username}.name; + userEmail = manifest.users.${config.home.username}.email; + signing.key = "~/.ssh/id_ed25519.pub"; + }; + }; +} diff --git a/nix/modules/home-manager.nix b/nix/modules/home-manager.nix index 2d6bf83..7b11f4d 100644 --- a/nix/modules/home-manager.nix +++ b/nix/modules/home-manager.nix @@ -1,14 +1,16 @@ { inputs, config, ... }: let - inherit (cfg.lib) flattenAttrs; + inherit (cfg.lib) forAllUsers' flattenAttrs; cfg = config.flake; hm = inputs.home-manager; globalCfg = { useGlobalPkgs = true; useUserPackages = true; + extraSpecialArgs = { inherit (cfg) manifest; }; sharedModules = [ (flattenAttrs (cfg.modules.homeManager or { })) ]; + users = forAllUsers' (name: _: cfg.homes.${name}); }; in { diff --git a/nix/modules/hosts.nix b/nix/modules/hosts.nix index ddcdf3c..c2a87e0 100644 --- a/nix/modules/hosts.nix +++ b/nix/modules/hosts.nix @@ -18,6 +18,7 @@ let nixosSystem { specialArgs = { inherit inputs; + inherit (cfg) manifest; hostName = name; }; modules = [ diff --git a/nix/modules/shell.nix b/nix/modules/shell.nix new file mode 100644 index 0000000..865212f --- /dev/null +++ b/nix/modules/shell.nix @@ -0,0 +1,22 @@ +{ config, lib, ... }: +let + cfg = config.flake; + inherit (cfg.lib) forAllUsers'; + inherit (lib.attrsets) mapAttrs'; +in +{ + flake.modules.nixos.default = + { pkgs, ... }: + { + programs = mapAttrs' (name: value: { + name = value.shell; + value.enable = true; + }) cfg.manifest.users; + users.users = forAllUsers' (_: value: { shell = pkgs.${value.shell}; }); + }; + flake.modules.homeManager.default = + { config, ... }: + { + programs.${cfg.manifest.users.${config.home.username}.shell}.enable = true; + }; +} diff --git a/nix/modules/users.nix b/nix/modules/users.nix index 6a423fd..471dd15 100644 --- a/nix/modules/users.nix +++ b/nix/modules/users.nix @@ -1,35 +1,31 @@ { config, lib, ... }: let cfg = config.flake; - inherit (config.flake.lib) forAllUsers flattenAttrs; - inherit (lib.attrsets) filterAttrs; - owner = flattenAttrs (filterAttrs (_: v: (v.primary or false)) cfg.manifest.users); + inherit (cfg.lib) forAllUsers'; + inherit (lib.lists) optional; in { flake.modules.nixos.default = - { pkgs, config, ... }: + { config, ... }: { #TODO: move sudo/security options elsewhere - security.sudo.wheelNeedsPassword = false; - nix.settings.trusted-users = [ "@wheel" ]; - #TODO: move to shell config - programs.${owner.shell}.enable = true; + # security.sudo.wheelNeedsPassword = false; + # nix.settings.trusted-users = [ "@wheel" ]; #TODO: move ssh key settings elsewhere + # users.users.root.openssh.authorizedKeys.keys = [ owner.pubkey ]; users = { mutableUsers = false; groups.users.gid = 100; - users.root.openssh.authorizedKeys.keys = [ owner.pubkey ]; - users.${owner.username} = { - isNormalUser = true; - # hashedPasswordFile - extraGroups = [ "wheel" ]; - shell = pkgs.${owner.shell}; - openssh.authorizedKeys.keys = [ owner.pubkey ]; - }; + users = forAllUsers' ( + _: value: { + isNormalUser = true; + extraGroups = optional (value.primary or false) "wheel"; + openssh.authorizedKeys.keys = [ value.pubkey ]; + } + ); }; - home-manager.users = forAllUsers ( + home-manager.users = forAllUsers' ( name: _: { - #TODO: move into nixos/darwin config - should not apply to homeConfigurations home.username = name; home.homeDirectory = config.users.users.${name}.home; }