Refactored code.

This commit is contained in:
samoht9277 2024-02-07 14:51:51 -03:00
parent b9d013b2f3
commit 3e7d032559
No known key found for this signature in database
GPG key ID: 612AC42768391756

View file

@ -1,43 +1,51 @@
#! /usr/bin/env bash #! /usr/bin/env bash
set -ex -o pipefail # More info at: https://github.com/elitak/nixos-infect
set -e -o pipefail
makeConf() { makeConf() {
# Skip everything if main config already present
[[ -e /etc/nixos/configuration.nix ]] && return 0 [[ -e /etc/nixos/configuration.nix ]] && return 0
# Lightsail config is not like the others
if [ "$PROVIDER" = "lightsail" ]; then if [ "$PROVIDER" = "lightsail" ]; then
makeLightsailConf makeLightsailConf
return 0 return 0
fi fi
# NB <<"EOF" quotes / $ ` in heredocs, <<EOF does not
mkdir -p /etc/nixos mkdir -p /etc/nixos
# Prevent grep for sending error code 1 (and halting execution) when no lines are selected : https://www.unix.com/man-page/posix/1P/grep
local IFS=$'\n'
for trypath in /root/.ssh/authorized_keys /home/$SUDO_USER/.ssh/authorized_keys $HOME/.ssh/authorized_keys; do
[[ -r "$trypath" ]] \
&& keys=$(sed -E 's/^.*((ssh|ecdsa)-[^[:space:]]+)[[:space:]]+([^[:space:]]+)([[:space:]]*.*)$/\1 \3\4/' "$trypath") \
&& [[ ! -z "$keys" ]] \
&& break
done
local network_import="" local network_import=""
[[ -n "$doNetConf" ]] && network_import="./networking.nix # generated at runtime by nixos-infect" [[ -n "$doNetConf" ]] && network_import="./networking.nix # generated at runtime by nixos-infect"
cat > /etc/nixos/configuration.nix << EOF cat > /etc/nixos/configuration.nix << EOF
{ config, pkgs, ... }: { config, pkgs, ... }:
{ {
imports = [ imports = [
./hardware-configuration.nix ./hardware-configuration.nix
$network_import $network_import
$NIXOS_IMPORT $NIXOS_IMPORT
]; ];
nix.settings = { nix.settings = {
# Enable flakes and new 'nix' command # Enable flakes and new 'nix' command
experimental-features = "nix-command flakes"; experimental-features = "nix-command flakes";
# Deduplicate and optimize nix store # Deduplicate and optimize nix store
auto-optimise-store = true; auto-optimise-store = true;
}; };
boot.loader.systemd-boot.enable = true; boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true; boot.loader.efi.canTouchEfiVariables = true;
zramSwap.enable = false; zramSwap.enable = false;
networking.hostName = "$(hostname -s)"; networking.hostName = "$(hostname -s)";
time.timeZone = "America/Argentina/Buenos_Aires"; time.timeZone = "America/Argentina/Buenos_Aires";
users.extraUsers.admin = { users.extraUsers.admin = {
isNormalUser = true; isNormalUser = true;
createHome = true; createHome = true;
@ -49,7 +57,6 @@ makeConf() {
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIH+Pn4eeMouj+BUj3ynUYzjvpxeepJC8GU3RFTE+eOch hetzner_lambda" "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIH+Pn4eeMouj+BUj3ynUYzjvpxeepJC8GU3RFTE+eOch hetzner_lambda"
]; ];
}; };
users.extraUsers.dev = { users.extraUsers.dev = {
isNormalUser = true; isNormalUser = true;
createHome = true; createHome = true;
@ -61,7 +68,6 @@ makeConf() {
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIH+Pn4eeMouj+BUj3ynUYzjvpxeepJC8GU3RFTE+eOch hetzner_lambda" "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIH+Pn4eeMouj+BUj3ynUYzjvpxeepJC8GU3RFTE+eOch hetzner_lambda"
]; ];
}; };
security.sudo.extraRules = [ security.sudo.extraRules = [
{ {
users = [ "admin" ]; users = [ "admin" ];
@ -73,7 +79,6 @@ makeConf() {
]; ];
} }
]; ];
environment.systemPackages = with pkgs; [ environment.systemPackages = with pkgs; [
curl curl
gnupg gnupg
@ -83,7 +88,6 @@ makeConf() {
unzip unzip
caddy caddy
]; ];
services.openssh = { services.openssh = {
enable = true; enable = true;
#settings = { #settings = {
@ -94,11 +98,9 @@ makeConf() {
# AllowTcpForwarding = false; # AllowTcpForwarding = false;
#}; #};
}; };
# networking.firewall.allowedTCPPorts = [ ... ]; # networking.firewall.allowedTCPPorts = [ ... ];
# networking.firewall.allowedUDPPorts = [ ... ]; # networking.firewall.allowedUDPPorts = [ ... ];
networking.firewall.enable = false; # Preferably, use provider's FW networking.firewall.enable = false; # Preferably, use provider's FW
system.stateVersion = "23.11"; # Did you read the comment? system.stateVersion = "23.11"; # Did you read the comment?
} }
EOF EOF
@ -438,20 +440,15 @@ fi
checkEnv checkEnv
prepareEnv prepareEnv
checkExistingSwap checkExistingSwap
if [[ -z "$NO_SWAP" ]]; then if [[ -z "$NO_SWAP" ]]; then
makeSwap # smallest (512MB) droplet needs extra memory! makeSwap # smallest (512MB) droplet needs extra memory!
fi fi
makeConf makeConf
infect infect
if [[ -z "$NO_SWAP" ]]; then if [[ -z "$NO_SWAP" ]]; then
removeSwap removeSwap
fi fi
if [[ -z "$NO_REBOOT" ]]; then if [[ -z "$NO_REBOOT" ]]; then
reboot reboot
fi fi
bootctl install --graceful # Add necessary boot files.