Add declarative ssh known hosts

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2025-02-25 12:28:22 +00:00
parent f843deafbe
commit a3dc4129d6
22 changed files with 258 additions and 79 deletions

6
flake.lock generated
View File

@@ -115,11 +115,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1740348184, "lastModified": 1740485474,
"narHash": "sha256-NnMzG2GYQJRrFTjvZBkaIE41EBekaMfIWiiEvxhvUTU=", "narHash": "sha256-g3f5UTD/VEZoSrvwXy1aW/3470Gz/M6vkucM+5f0ZkU=",
"owner": "karaolidis", "owner": "karaolidis",
"repo": "home-manager", "repo": "home-manager",
"rev": "6db31ab82b2b0d6bad4691a238073401120f673c", "rev": "c4ace2196b7df8f582e624b2b54ec5a7ab353549",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@@ -168,7 +168,9 @@
in in
{ {
devShells = import ./hosts/common/shells { inherit pkgs; }; devShells = import ./hosts/common/shells { inherit pkgs; };
packages = import ./packages { inherit pkgs; }; lib = import ./lib { inherit pkgs; };
packages = import ./packages { inherit pkgs inputs system; };
formatter = treefmt.config.build.wrapper; formatter = treefmt.config.build.wrapper;
checks.formatting = treefmt.config.build.check self; checks.formatting = treefmt.config.build.check self;
} }

View File

@@ -4,14 +4,5 @@
}: }:
{ ... }: { ... }:
{ {
environment.persistence."/persist"."${home}/.ssh/known_hosts" = { }; home-manager.users.${user}.programs.ssh.enable = true;
home-manager.users.${user} = {
programs.ssh = {
enable = true;
userKnownHostsFile = "${home}/.ssh/known_hosts/default";
};
systemd.user.tmpfiles.rules = [ "d ${home}/.ssh/known_hosts 0755 ${user} users" ];
};
} }

View File

@@ -10,6 +10,7 @@
... ...
}: }:
let let
selfLib = inputs.self.lib.${system};
hmConfig = config.home-manager.users.${user}; hmConfig = config.home-manager.users.${user};
in in
{ {
@@ -64,7 +65,7 @@ in
"spotify/prefs.init" = { "spotify/prefs.init" = {
source = ./config/prefs; source = ./config/prefs;
onChange = '' onChange = ''
${config.lib.runtime.merge.keyValue} "${home}/.config/spotify/prefs.init" "${home}/.config/spotify/prefs" ${selfLib.runtime.merge.keyValue} "${home}/.config/spotify/prefs.init" "${home}/.config/spotify/prefs"
''; '';
}; };
@@ -72,7 +73,7 @@ in
source = ./config/prefs-user; source = ./config/prefs-user;
onChange = '' onChange = ''
user = $(cat "${hmConfig.sops.secrets."spotify/username".path}") user = $(cat "${hmConfig.sops.secrets."spotify/username".path}")
${config.lib.runtime.merge.keyValue} "${home}/.config/spotify/prefs-user.init" "${home}/.config/spotify/Users/''${user}-user/prefs" ${selfLib.runtime.merge.keyValue} "${home}/.config/spotify/prefs-user.init" "${home}/.config/spotify/Users/''${user}-user/prefs"
''; '';
}; };
}; };

View File

@@ -1,8 +1,6 @@
{ inputs, ... }: { inputs, ... }:
{ {
imports = [ imports = [
../../lib
inputs.disko.nixosModules.disko inputs.disko.nixosModules.disko
./format.nix ./format.nix

View File

@@ -2,7 +2,15 @@
user ? throw "user argument is required", user ? throw "user argument is required",
home ? throw "home argument is required", home ? throw "home argument is required",
}: }:
{ ... }: {
inputs,
lib,
system,
...
}:
let
selfPkgs = inputs.self.packages.${system};
in
{ {
home-manager.users.${user} = { home-manager.users.${user} = {
sops.secrets = { sops.secrets = {
@@ -17,12 +25,16 @@
}; };
}; };
programs.ssh.matchBlocks = { programs.ssh = {
matchBlocks = {
"github.com" = { "github.com" = {
hostname = "github.com"; hostname = "github.com";
user = "git"; user = "git";
identityFile = "${home}/.ssh/ssh_personal_ed25519_key"; identityFile = "${home}/.ssh/ssh_personal_ed25519_key";
}; };
}; };
userKnownHostsFiles = with selfPkgs; [ ssh-known-hosts-github ];
};
}; };
} }

View File

@@ -1,4 +1,7 @@
{ ... }: { inputs, system, ... }:
let
selfPkgs = inputs.self.packages.${system};
in
{ {
sops.secrets."ssh/sas/key" = { sops.secrets."ssh/sas/key" = {
sopsFile = ../../../../secrets/sas/secrets.yaml; sopsFile = ../../../../secrets/sas/secrets.yaml;
@@ -6,7 +9,8 @@
path = "/root/.ssh/ssh_sas_ed25519_key"; path = "/root/.ssh/ssh_sas_ed25519_key";
}; };
programs.ssh.extraConfig = '' programs.ssh = {
extraConfig = ''
Host github.com Host github.com
User git User git
HostName github.com HostName github.com
@@ -17,4 +21,10 @@
HostName gitlab.sas.com HostName gitlab.sas.com
IdentityFile /root/.ssh/ssh_sas_ed25519_key IdentityFile /root/.ssh/ssh_sas_ed25519_key
''; '';
knownHostsFiles = with selfPkgs; [
ssh-known-hosts-github
ssh-known-hosts-sas-gitlab
];
};
} }

View File

@@ -1,8 +1,6 @@
{ config, inputs, ... }: { config, inputs, ... }:
{ {
imports = [ imports = [
../../lib
inputs.disko.nixosModules.disko inputs.disko.nixosModules.disko
./format.nix ./format.nix

View File

@@ -2,7 +2,16 @@
user ? throw "user argument is required", user ? throw "user argument is required",
home ? throw "home argument is required", home ? throw "home argument is required",
}: }:
{ lib, pkgs, ... }: {
inputs,
lib,
system,
pkgs,
...
}:
let
selfPkgs = inputs.self.packages.${system};
in
{ {
home-manager.users.${user} = { home-manager.users.${user} = {
sops.secrets = { sops.secrets = {
@@ -26,7 +35,8 @@
} }
); );
ssh.matchBlocks = { ssh = {
matchBlocks = {
"github.com" = { "github.com" = {
hostname = "github.com"; hostname = "github.com";
user = "git"; user = "git";
@@ -49,6 +59,13 @@
identityFile = "${home}/.ssh/ssh_sas_ed25519_key"; identityFile = "${home}/.ssh/ssh_sas_ed25519_key";
}; };
}; };
userKnownHostsFiles = with selfPkgs; [
ssh-known-hosts-github
ssh-known-hosts-sas-gitlab
ssh-known-hosts-sas-gerrit
];
};
}; };
}; };
} }

View File

@@ -2,13 +2,20 @@
user ? throw "user argument is required", user ? throw "user argument is required",
home ? throw "home argument is required", home ? throw "home argument is required",
}: }:
{ ... }: { inputs, system, ... }:
let
selfPkgs = inputs.self.packages.${system};
in
{ {
home-manager.users.${user}.programs.ssh.matchBlocks = { home-manager.users.${user}.programs.ssh = {
matchBlocks = {
"cldlgn.fyi.sas.com" = { "cldlgn.fyi.sas.com" = {
inherit user; inherit user;
hostname = "cldlgn.fyi.sas.com"; hostname = "cldlgn.fyi.sas.com";
identityFile = "${home}/.ssh/ssh_sas_ed25519_key"; identityFile = "${home}/.ssh/ssh_sas_ed25519_key";
}; };
}; };
userKnownHostsFiles = with selfPkgs; [ ssh-known-hosts-sas-cldlgn ];
};
} }

View File

@@ -1,8 +1,6 @@
{ config, inputs, ... }: { config, inputs, ... }:
{ {
imports = [ imports = [
../../lib
inputs.disko.nixosModules.disko inputs.disko.nixosModules.disko
./format.nix ./format.nix

View File

@@ -2,7 +2,15 @@
user ? throw "user argument is required", user ? throw "user argument is required",
home ? throw "home argument is required", home ? throw "home argument is required",
}: }:
{ ... }: {
inputs,
lib,
system,
...
}:
let
selfPkgs = inputs.self.packages.${system};
in
{ {
home-manager.users.${user} = { home-manager.users.${user} = {
sops.secrets = { sops.secrets = {
@@ -17,12 +25,16 @@
}; };
}; };
programs.ssh.matchBlocks = { programs.ssh = {
matchBlocks = {
"github.com" = { "github.com" = {
hostname = "github.com"; hostname = "github.com";
user = "git"; user = "git";
identityFile = "${home}/.ssh/ssh_personal_ed25519_key"; identityFile = "${home}/.ssh/ssh_personal_ed25519_key";
}; };
}; };
userKnownHostsFiles = with selfPkgs; [ ssh-known-hosts-github ];
};
}; };
} }

View File

@@ -1,6 +1,5 @@
{ pkgs, ... }: { pkgs, ... }:
{ {
lib = { fetchers = import ./fetchers { inherit pkgs; };
runtime = import ./runtime { inherit pkgs; }; runtime = import ./runtime { inherit pkgs; };
};
} }

4
lib/fetchers/default.nix Normal file
View File

@@ -0,0 +1,4 @@
{ pkgs, ... }:
{
sshKnownHosts = import ./sshKnownHosts { inherit pkgs; };
}

View File

@@ -0,0 +1,33 @@
{ pkgs, ... }:
pkgs.lib.fetchers.withNormalizedHash { } (
{
host,
name ? "ssh-known-hosts-${host}",
outputHash,
outputHashAlgo,
port ? 22,
keyTypes ? [
"rsa"
"ecdsa"
"ed25519"
],
}:
let
keyTypeArgs = pkgs.lib.concatStringsSep "," keyTypes;
in
pkgs.runCommand name
{
inherit outputHash outputHashAlgo;
outputHashMode = "flat";
preferLocalBuild = true;
nativeBuildInputs = with pkgs; [
openssh
gnugrep
coreutils
];
}
''
ssh-keyscan -p ${toString port} -t ${keyTypeArgs} ${host} | grep -v '^#' | sort > $out
''
)

View File

@@ -148,8 +148,6 @@ cat <<EOF > "./hosts/$host/default.nix"
{ inputs, ... }: { inputs, ... }:
{ {
imports = [ imports = [
../../lib
inputs.disko.nixosModules.disko inputs.disko.nixosModules.disko
./format.nix ./format.nix

View File

@@ -1,4 +1,9 @@
{ pkgs, ... }: {
pkgs,
inputs,
system,
...
}:
{ {
darktable-ghost-cms-publish = import ./darktable/ghost-cms-publish { inherit pkgs; }; darktable-ghost-cms-publish = import ./darktable/ghost-cms-publish { inherit pkgs; };
darktable-hald-clut = import ./darktable/hald-clut { inherit pkgs; }; darktable-hald-clut = import ./darktable/hald-clut { inherit pkgs; };
@@ -22,7 +27,13 @@
obsidian-theme-minimal = import ./obsidian/themes/minimal { inherit pkgs; }; obsidian-theme-minimal = import ./obsidian/themes/minimal { inherit pkgs; };
ssh-known-hosts-github = import ./ssh/known-hosts/github { inherit pkgs inputs system; };
# SAS # SAS
ssh-known-hosts-sas-cldlgn = import ./ssh/known-hosts/sas/cldlgn { inherit pkgs inputs system; };
ssh-known-hosts-sas-gerrit = import ./ssh/known-hosts/sas/gerrit { inherit pkgs inputs system; };
ssh-known-hosts-sas-gitlab = import ./ssh/known-hosts/sas/gitlab { inherit pkgs inputs system; };
viya4-ark = import ./sas/viya4-ark { inherit pkgs; }; viya4-ark = import ./sas/viya4-ark { inherit pkgs; };
viya4-orders-cli = import ./sas/viya4-orders-cli { inherit pkgs; }; viya4-orders-cli = import ./sas/viya4-orders-cli { inherit pkgs; };

View File

@@ -0,0 +1,22 @@
{
pkgs,
inputs,
system,
...
}:
# AUTO-UPDATE: echo "Warning: Package using custom fetcher cannot be automatically updated." >&2
pkgs.stdenv.mkDerivation rec {
pname = "ssh-known-hosts-github";
version = "0-unstable-2025-02-25";
src = inputs.self.lib.${system}.fetchers.sshKnownHosts {
host = "github.com";
hash = "sha256-wkNdynz7rhZvfXSAXDpQ2sk40afKAPeYHQ8Ei44CICI=";
};
phases = [ "installPhase" ];
installPhase = ''
cp $src $out
'';
}

View File

@@ -0,0 +1,22 @@
{
pkgs,
inputs,
system,
...
}:
# AUTO-UPDATE: echo "Warning: Package using custom fetcher cannot be automatically updated." >&2
pkgs.stdenv.mkDerivation rec {
pname = "ssh-known-hosts-sas-cldlgn";
version = "0-unstable-2025-02-25";
src = inputs.self.lib.${system}.fetchers.sshKnownHosts {
host = "cldlgn.fyi.sas.com";
hash = "sha256-HymFic00RROW1tC4sQe5QdDM7D8IDeTdKe8rWU6xhZM=";
};
phases = [ "installPhase" ];
installPhase = ''
cp $src $out
'';
}

View File

@@ -0,0 +1,22 @@
{
pkgs,
inputs,
system,
...
}:
# AUTO-UPDATE: echo "Warning: Package using custom fetcher cannot be automatically updated." >&2
pkgs.stdenv.mkDerivation rec {
pname = "ssh-known-hosts-sas-gerrit";
version = "0-unstable-2025-02-25";
src = inputs.self.lib.${system}.fetchers.sshKnownHosts {
host = "gerrit-svi.unx.sas.com";
hash = "sha256-+lvC19RyBWFhEwEdXIb/xwEyGuKnatkgOsmhAc583kA=";
};
phases = [ "installPhase" ];
installPhase = ''
cp $src $out
'';
}

View File

@@ -0,0 +1,22 @@
{
pkgs,
inputs,
system,
...
}:
# AUTO-UPDATE: echo "Warning: Package using custom fetcher cannot be automatically updated." >&2
pkgs.stdenv.mkDerivation rec {
pname = "ssh-known-hosts-sas-gitlab";
version = "0-unstable-2025-02-25";
src = inputs.self.lib.${system}.fetchers.sshKnownHosts {
host = "gitlab.sas.com";
hash = "sha256-gJGM6bG+u+XS2UdyYtK7MXP2r8w3tX/1kJmsDpyFKWI=";
};
phases = [ "installPhase" ];
installPhase = ''
cp $src $out
'';
}