From f45e2d61f4aa0ed9eb5d0d2ce379b6de8028b575 Mon Sep 17 00:00:00 2001 From: cnst Date: Thu, 3 Jul 2025 11:04:42 +0200 Subject: [PATCH] modularize git --- modules/default.nix | 1 + modules/home/programs/git/default.nix | 59 +++++++++++++++++++++++++ modules/options/accounts/default.nix | 35 +++++++++++++-- users/cnst/default.nix | 1 - users/cnst/git.nix | 19 ++------ users/cnst/modules/home.nix | 3 ++ users/cnst/modules/options.nix | 3 +- users/cnstlab/default.nix | 1 - users/cnstlab/git.nix | 62 --------------------------- users/cnstlab/modules/options.nix | 3 +- users/toothpick/default.nix | 1 - users/toothpick/git.nix | 7 --- 12 files changed, 103 insertions(+), 92 deletions(-) create mode 100644 modules/home/programs/git/default.nix delete mode 100644 users/cnstlab/git.nix delete mode 100644 users/toothpick/git.nix diff --git a/modules/default.nix b/modules/default.nix index c01883a4..a962e7ab 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -14,6 +14,7 @@ ./home/programs/foot ./home/programs/fuzzel ./home/programs/ghostty + ./home/programs/git ./home/programs/helix ./home/programs/hyprlock ./home/programs/jujutsu diff --git a/modules/home/programs/git/default.nix b/modules/home/programs/git/default.nix new file mode 100644 index 00000000..14050568 --- /dev/null +++ b/modules/home/programs/git/default.nix @@ -0,0 +1,59 @@ +{ + config, + pkgs, + lib, + ... +}: let + inherit (lib) mkIf mkEnableOption; + cfg = config.home.programs.git; +in { + options = { + home.programs.git.enable = mkEnableOption "Enables git"; + }; + config = mkIf cfg.enable { + home.packages = [pkgs.gh]; + programs.git = { + enable = true; + userName = config.accounts.username; + userEmail = config.accounts.mail; + delta = { + enable = true; + options.dark = true; + }; + extraConfig = { + # user.signingkey = "${config.home.homeDirectory}/.ssh/id_ed25519.pub"; + user.signingkey = "${config.home.homeDirectory}/.config/git/allowed_signers"; + signing = { + format = lib.mkDefault "ssh"; + key = "${config.home.homeDirectory}/.ssh/id_ed25519"; + signByDefault = true; + }; + gpg = { + # format = lib.mkDefault "ssh"; + ssh.allowedSignersFile = config.home.homeDirectory + "/" + config.xdg.configFile."git/allowed_signers".target; + }; + commit = { + verbose = true; + gpgSign = false; + }; + init.defaultBranch = "main"; + merge.conflictStyle = "diff3"; + diff.algorithm = "histogram"; + log.date = "iso"; + column.ui = "auto"; + branch.sort = "committerdate"; + push.autoSetupRemote = true; + rerere.enabled = true; + }; + lfs.enable = true; + ignores = [ + ".direnv" + "result" + ".jj" + ]; + }; + xdg.configFile."git/allowed_signers".text = '' + ${config.accounts.mail} namespaces="git" ${config.accounts.sshKey} + ''; + }; +} diff --git a/modules/options/accounts/default.nix b/modules/options/accounts/default.nix index 6f79fc9a..64a1fac6 100644 --- a/modules/options/accounts/default.nix +++ b/modules/options/accounts/default.nix @@ -4,6 +4,24 @@ ... }: let inherit (lib) mkOption types; + + sshKeys = { + cnixpad = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIXCjkKouZrsMoswMIeueO8X/c3kuY3Gb0E9emvkqwUv cnst@cnixpad"; + cnixlab = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICiNcNex+/hrEQJYJJTj89uPXocSfChU38E5TujWdxaM cnstlab@cnixlab"; + cnixtop = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEUub8vbzUn2f39ILhAJ2QeH8xxLSjiyUuo8xvHGx/VB adam@cnst.dev"; + toothpc = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGu5vZbb5ExampleKeyHereGfDF9c5 toothpick@toothpc"; + }; + + keyName = config.accounts.sshUser or null; + + selectedKey = + if keyName != null + then + lib.attrByPath [keyName] ( + builtins.abort "No SSH key defined for hostname/key '${toString keyName}'" + ) + sshKeys + else builtins.abort "No accounts.sshUser provided, cannot select SSH key."; in { options.accounts = { username = mkOption { @@ -11,10 +29,21 @@ in { default = "cnst"; description = "Set the desired username"; }; - hostname = mkOption { + mail = mkOption { type = types.str; - default = "cnix"; - description = "Set the desired hostname"; + default = "adam@cnst.dev"; + description = "Set the desired email"; + }; + sshKey = lib.mkOption { + type = lib.types.str; + default = selectedKey; + description = "Host-specific SSH key"; + readOnly = true; + }; + sshUser = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + description = "Optional override for selecting an SSH key by name"; }; }; } diff --git a/users/cnst/default.nix b/users/cnst/default.nix index b2711050..a15855ef 100644 --- a/users/cnst/default.nix +++ b/users/cnst/default.nix @@ -11,7 +11,6 @@ { imports = [ ./modules - ./git.nix ]; # ++ lib.optionals isCnixpad [./cpmodules.nix]; diff --git a/users/cnst/git.nix b/users/cnst/git.nix index ba923aa1..1b420aa1 100644 --- a/users/cnst/git.nix +++ b/users/cnst/git.nix @@ -1,25 +1,14 @@ { config, pkgs, - osConfig, lib, ... -}: let - email = config.programs.git.userEmail; - isCnixpad = osConfig.networking.hostName == "cnixpad"; - isCnixlab = osConfig.networking.hostName == "cnixlab"; - sshKey = - if isCnixpad - then "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIXCjkKouZrsMoswMIeueO8X/c3kuY3Gb0E9emvkqwUv cnst@cnixpad" - else if isCnixlab - then "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICiNcNex+/hrEQJYJJTj89uPXocSfChU38E5TujWdxaM cnstlab@cnixlab" - else "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEUub8vbzUn2f39ILhAJ2QeH8xxLSjiyUuo8xvHGx/VB adam@cnst.dev"; -in { +}: { home.packages = [pkgs.gh]; programs.git = { enable = true; - userName = "cnst"; - userEmail = "adam@cnst.dev"; + userName = config.accounts.username; + userEmail = config.accounts.mail; delta = { enable = true; options.dark = true; @@ -57,6 +46,6 @@ in { ]; }; xdg.configFile."git/allowed_signers".text = '' - ${email} namespaces="git" ${sshKey} + ${config.accounts.mail} namespaces="git" ${config.accounts.sshKey} ''; } diff --git a/users/cnst/modules/home.nix b/users/cnst/modules/home.nix index 0aa89f30..ce5f6da9 100644 --- a/users/cnst/modules/home.nix +++ b/users/cnst/modules/home.nix @@ -35,6 +35,9 @@ fuzzel = { enable = true; }; + git = { + enable = true; + }; ghostty = { enable = true; }; diff --git a/users/cnst/modules/options.nix b/users/cnst/modules/options.nix index ea098019..9f85a72a 100644 --- a/users/cnst/modules/options.nix +++ b/users/cnst/modules/options.nix @@ -1,7 +1,8 @@ { accounts = { username = "cnst"; - hostname = "cnixtop"; + mail = "adam@cnst.dev"; + sshUser = "cnixtop"; }; monitors = [ { diff --git a/users/cnstlab/default.nix b/users/cnstlab/default.nix index 57d47994..91f7e119 100644 --- a/users/cnstlab/default.nix +++ b/users/cnstlab/default.nix @@ -11,7 +11,6 @@ { imports = [ ./modules - ./git.nix ]; # ++ lib.optionals isCnixpad [./cpmodules.nix]; diff --git a/users/cnstlab/git.nix b/users/cnstlab/git.nix deleted file mode 100644 index ba923aa1..00000000 --- a/users/cnstlab/git.nix +++ /dev/null @@ -1,62 +0,0 @@ -{ - config, - pkgs, - osConfig, - lib, - ... -}: let - email = config.programs.git.userEmail; - isCnixpad = osConfig.networking.hostName == "cnixpad"; - isCnixlab = osConfig.networking.hostName == "cnixlab"; - sshKey = - if isCnixpad - then "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIXCjkKouZrsMoswMIeueO8X/c3kuY3Gb0E9emvkqwUv cnst@cnixpad" - else if isCnixlab - then "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICiNcNex+/hrEQJYJJTj89uPXocSfChU38E5TujWdxaM cnstlab@cnixlab" - else "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEUub8vbzUn2f39ILhAJ2QeH8xxLSjiyUuo8xvHGx/VB adam@cnst.dev"; -in { - home.packages = [pkgs.gh]; - programs.git = { - enable = true; - userName = "cnst"; - userEmail = "adam@cnst.dev"; - delta = { - enable = true; - options.dark = true; - }; - extraConfig = { - # user.signingkey = "${config.home.homeDirectory}/.ssh/id_ed25519.pub"; - user.signingkey = "${config.home.homeDirectory}/.config/git/allowed_signers"; - signing = { - format = lib.mkDefault "ssh"; - key = "${config.home.homeDirectory}/.ssh/id_ed25519"; - signByDefault = true; - }; - gpg = { - # format = lib.mkDefault "ssh"; - ssh.allowedSignersFile = config.home.homeDirectory + "/" + config.xdg.configFile."git/allowed_signers".target; - }; - commit = { - verbose = true; - gpgSign = false; - }; - init.defaultBranch = "main"; - merge.conflictStyle = "diff3"; - diff.algorithm = "histogram"; - log.date = "iso"; - column.ui = "auto"; - branch.sort = "committerdate"; - push.autoSetupRemote = true; - rerere.enabled = true; - }; - lfs.enable = true; - ignores = [ - ".direnv" - "result" - ".jj" - ]; - }; - xdg.configFile."git/allowed_signers".text = '' - ${email} namespaces="git" ${sshKey} - ''; -} diff --git a/users/cnstlab/modules/options.nix b/users/cnstlab/modules/options.nix index cd173003..7540b58c 100644 --- a/users/cnstlab/modules/options.nix +++ b/users/cnstlab/modules/options.nix @@ -1,7 +1,8 @@ { accounts = { username = "cnstlab"; - hostname = "cnixlab"; + mail = "adam@cnst.dev"; + sshUser = "cnixlab"; }; monitors = [ { diff --git a/users/toothpick/default.nix b/users/toothpick/default.nix index 3385b498..a57ff9d7 100644 --- a/users/toothpick/default.nix +++ b/users/toothpick/default.nix @@ -1,7 +1,6 @@ {pkgs, ...}: { imports = [ ./modules - ./git.nix ]; home = { username = "toothpick"; diff --git a/users/toothpick/git.nix b/users/toothpick/git.nix deleted file mode 100644 index 245b47e8..00000000 --- a/users/toothpick/git.nix +++ /dev/null @@ -1,7 +0,0 @@ -{ - programs.git = { - enable = true; - userName = "toothpick"; - userEmail = "fredrik@libellux.com"; - }; -}