fix(ssh): use home-manager to manage ssh config

This commit is contained in:
spy4x 2024-03-15 15:21:36 +08:00
parent bd7556a682
commit 46773dfbe0
6 changed files with 44 additions and 27 deletions

2
.gitignore vendored
View File

@ -2,4 +2,4 @@
.idea/
# Ignore private configurations
private/ssh-config.nix
private/ssh-config

View File

@ -2,17 +2,18 @@
In this repo I store my config files.
It helps me to install quickly all software I need for work and fun.
Feel free to check & alter the list of software to be installed in these files.
Feel free to check & alter the configs as you like.
I use NixOS. It stores all of it's configuration in `configuration.nix`.
That's where you can start exploration and modification.
I use NixOS. It stores all of its configuration in `configuration.nix`.
Start your exploration there.
If you are confused at any step - checkout docs on https://nixos.org/
## Install
1. Clone this repo.
2. Give build script permission to execute: `chmod +x ./build-nix.sh`.
2. [Optionally] SSH Config `cp ./ssh-config.nix.example ./ssh-config.nix` and fill with your values.
3. Apply configuration with `make`.
3. [Optionally] Check example files in `./private` folder. If anything there you'd like to use - copy example file and remove ".example" suffix. Fill file with your content. Example: SSH Config `cp ./private/ssh-config.nix.example ./private/ssh-config.nix` and fill with your values.
4. Apply configuration with `make`.
## Usage

View File

@ -12,7 +12,9 @@ sudo -v
echo "Copying new configuration..."
# Copy the local configuration.nix to the system configuration location
sudo cp ./*.nix "${NIXOS_CONFIG_PATH}"
sudo cp ./configuration.nix "${NIXOS_CONFIG_PATH}"
sudo mkdir -p "${NIXOS_CONFIG_PATH}/private"
sudo cp ./private/* "${NIXOS_CONFIG_PATH}/private/"
echo "Rebuilding NixOS configuration..."
# Rebuild the NixOS configuration and make the changes effective

View File

@ -5,23 +5,29 @@
{ config, pkgs, lib, ... }:
let
sshConfigPath = ./ssh-config.nix;
gdrivePath = "/home/spy4x/gdrive";
nixFolder = "/etc/nixos";
username = "spy4x";
userFullName = "Anton Shubin";
sshConfigPath = "${nixFolder}/private/ssh-config";
sshConfig = if builtins.pathExists "${sshConfigPath}" then builtins.readFile "${sshConfigPath}" else "# private ssh config file didn't exist to insert it's content here";
gdrivePath = "/home/${username}/gdrive";
curBin = "/run/current-system/sw/bin";
home-manager = builtins.fetchTarball "https://github.com/nix-community/home-manager/archive/release-23.11.tar.gz";
in
{
imports =
[
# Include the results of the hardware scan.
./hardware-configuration.nix
] ++ lib.optional (builtins.pathExists sshConfigPath) sshConfigPath;
(import "${home-manager}/nixos")
];
# Bootloader.
boot.loader.grub.enable = true;
boot.loader.grub.device = "/dev/nvme0n1";
boot.loader.grub.useOSProber = true;
networking.hostName = "spy4x-pc";
networking.hostName = "${username}-pc";
networking.networkmanager.enable = true;
networking = {
firewall = {
@ -92,10 +98,24 @@ in
hardware.logitech.wireless.enable = true;
hardware.logitech.wireless.enableGraphical = true;
home-manager.users.spy4x = {
home.stateVersion = "23.11";
home.username = username;
home.homeDirectory = "/home/${username}";
home.packages = with pkgs; [
wget
];
programs.ssh.enable = true;
programs.ssh.extraConfig = ''
${sshConfig}
'';
};
# Define a user account. Don't forget to set a password with passwd.
users.users.spy4x = {
isNormalUser = true;
description = "Anton Shubin";
description = userFullName;
extraGroups = [
"networkmanager"
"wheel"
@ -139,14 +159,13 @@ in
# Shell aliases and other init
environment.interactiveShellInit = ''
alias copy='wl-copy <'
alias build='sudo nixos-rebuild switch'
alias rs='rsync -avhzru -P'
alias rsh='rsync -avhzru -P -e ssh'
'';
# Enable automatic login for the user.
services.xserver.displayManager.autoLogin.enable = true;
services.xserver.displayManager.autoLogin.user = "spy4x";
services.xserver.displayManager.autoLogin.user = username;
# RClone Google Drive service
systemd.services.rclone-gdrive-mount = {
@ -163,7 +182,7 @@ in
ExecStop = "${curBin}/fusermount -u ${gdrivePath}";
Restart = "on-failure";
RestartSec = "10s";
User = "spy4x";
User = username;
Group = "users";
Environment = [ "PATH=/run/wrappers/bin/:$PATH" ]; # Required environments
};

View File

@ -0,0 +1,7 @@
# Part of config, to remote machine aliases for SSH
Host my-remote-machine # alias to do "ssh my remote-machine"
HostName 192.192.192.192 # ip/hostname
User my-user # username
# ... other hosts

View File

@ -1,12 +0,0 @@
# Part of config, to remote machine aliases for SSH
{ ... }: {
programs.ssh.extraConfig = ''
Host my-remote-machine # alias to do "ssh my remote-machine"
HostName 192.192.192.192 # ip/hostname
User my-user # username
# ... other hosts
'';
}