From 6a3f48279245b589cd6bec61cc0fa19051efb986 Mon Sep 17 00:00:00 2001 From: Greg Hellings Date: Fri, 11 Oct 2024 12:58:03 -0500 Subject: [PATCH] Continue moving personal packages from overlays Continue moving out of the overlay model for everything and into running sutff directly out of the packages. This should give a cleaner separation of the things that I don't need to maintain separately --- overlays/aacs.nix | 10 ---- overlays/create_ssl.nix | 28 ------------ overlays/default.nix | 6 --- overlays/inject-darwin.nix | 31 ------------- overlays/inject.nix | 55 ---------------------- overlays/setup-ssh/default.nix | 3 -- overlays/upgrade-pg-cluster.nix | 25 ---------- pkgs/aacs.nix | 14 ++++++ pkgs/create_ssl.nix | 32 +++++++++++++ pkgs/default.nix | 6 +++ pkgs/inject-darwin.nix | 38 +++++++++++++++ pkgs/inject.nix | 56 +++++++++++++++++++++++ pkgs/setup-ssh/default.nix | 7 +++ {overlays => pkgs}/setup-ssh/setup-ssh.sh | 2 +- pkgs/upgrade-pg-cluster.nix | 36 +++++++++++++++ 15 files changed, 190 insertions(+), 159 deletions(-) delete mode 100644 overlays/aacs.nix delete mode 100644 overlays/create_ssl.nix delete mode 100644 overlays/inject-darwin.nix delete mode 100644 overlays/inject.nix delete mode 100644 overlays/setup-ssh/default.nix delete mode 100644 overlays/upgrade-pg-cluster.nix create mode 100644 pkgs/aacs.nix create mode 100644 pkgs/create_ssl.nix create mode 100644 pkgs/inject-darwin.nix create mode 100644 pkgs/inject.nix create mode 100644 pkgs/setup-ssh/default.nix rename {overlays => pkgs}/setup-ssh/setup-ssh.sh (84%) create mode 100644 pkgs/upgrade-pg-cluster.nix diff --git a/overlays/aacs.nix b/overlays/aacs.nix deleted file mode 100644 index 6a1bd86..0000000 --- a/overlays/aacs.nix +++ /dev/null @@ -1,10 +0,0 @@ -{ pkgs, ... }: - -pkgs.writeShellScriptBin "aacs" '' - set -ex - [ ! -d "''${HOME}/.config/aacs" ] && mkdir -p "''${HOME}/.config/aacs" - cd "''${HOME}/.config/aacs" - [ -f KEYDB.cfg.zip ] && rm -f KEYDB.cfg.zip - curl -L -o KEYDB.cfg.zip "http://fvonline-db.bplaced.net/fv_download.php?lang=eng" - ${pkgs.unzip}/bin/unzip KEYDB.cfg.zip - mv keydb.cfg KEYDB.cfg'' diff --git a/overlays/create_ssl.nix b/overlays/create_ssl.nix deleted file mode 100644 index 25f7b12..0000000 --- a/overlays/create_ssl.nix +++ /dev/null @@ -1,28 +0,0 @@ -{ writeShellScriptBin, openssl, ... }: - -writeShellScriptBin "create_ssl" '' - set -e -o pipefail - name="''${1}" - root_key="''${2}" - - function usage { - echo "USAGE: create_ssl " - } - - if [ -z "''${name}" ]; then - usage - exit 1 - fi - - if [ -z "''${root_key}" ]; then - usage - exit 1 - fi - - # Create the certificate key - ${openssl}/bin/openssl ecparam -out "''${name}.key" -name prime256v1 -genkey - # Create the CSR - ${openssl}/bin/openssl req -name -sha256 -key "''${name}.key" -out "''${name}.csr" - # Sign it - ${openssl}/bin/openssl x509 -req -in "''${name}.csr" -CA "''${root_key}.crt" -CAkey "''${root_key}.key" -CAcreateserial -out "''${name}.crt" -days 3650 -sha256 -'' diff --git a/overlays/default.nix b/overlays/default.nix index 5ca09c3..86f0952 100644 --- a/overlays/default.nix +++ b/overlays/default.nix @@ -40,13 +40,7 @@ rec { ]; # My own packages - aacs = prev.callPackage ./aacs.nix { }; - create_ssl = prev.callPackage ./create_ssl.nix { }; enwiki-dump = prev.callPackage ./enwiki-dump.nix { }; - inject = prev.callPackage ./inject.nix { inherit (final) pkgs; }; - inject-darwin = prev.callPackage ./inject-darwin.nix { inherit (final) pkgs; }; - setup-ssh = prev.callPackage ./setup-ssh { pkgs = final.pkgs; }; - upgrade-pg-cluster = prev.callPackage ./upgrade-pg-cluster.nix { }; # Overrides of packages brew = prev.callPackage ./homebrew.nix { }; diff --git a/overlays/inject-darwin.nix b/overlays/inject-darwin.nix deleted file mode 100644 index c74d113..0000000 --- a/overlays/inject-darwin.nix +++ /dev/null @@ -1,31 +0,0 @@ -{ pkgs -, coreutils -, curl -, gnutar -, nix -, ... -}: -pkgs.writeShellScriptBin "inject-darwin" '' - set -ex - set -o pipefail - - dir="$(${coreutils}/bin/mktemp -d)" - cd "''${dir}" - - # Install nix-darwin - ${nix}/bin/nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A installer - ./result/bin/darwin-installer - - # Get my configuration - mkdir -p ~/.config/darwin - cd ~/.config/darwin - ${curl}/bin/curl -O -L https://github.com/greg-hellings/nixos-config/archive/refs/heads/main.tar.gz - ${gnutar}/bin/tar xvzf main.tar.gz --strip-components 1 - - # Build NixOS for this system - pushd "''${dir}" - ${nix}/bin/nix build "~/.config/darwin#darwinConfigurations.$(hostname -s).system" - ./result/sw/bin/darwin-rebuild switch --flake ~/.config/darwin - popd - rm -r "''${dir}" -'' diff --git a/overlays/inject.nix b/overlays/inject.nix deleted file mode 100644 index 04c382f..0000000 --- a/overlays/inject.nix +++ /dev/null @@ -1,55 +0,0 @@ -{ pkgs -, git -, ... -}: - -pkgs.writeShellScriptBin "inject-nixos-config" '' - hostname="''${1}" - if [ -n "''${hostname}"]; then - echo "You must provide a hostname"; - exit 1; - fi - - mv /etc/nixos /etc/nixos.bk - cd /etc - ${git}/bin/git clone http://github.com/greg-hellings/nixos-config nixos - mkdir -p "/etc/nixos/hosts/''${hostname}" - - # Prepares everything for the flake usage - #cp /etc/nixos.bk/configuration.nix "/etc/nixos/hosts/''${hostname}/default.nix" - cat << EOF > "/etc/nixos/hosts/''${hostname}/default.nix" - { pkgs, config, ... }: - - { - imports = [ ./hardware-configuration.nix ]; - - boot.loader = { - systemd-boot.enable = true; - efi.canTouchEfiVariables = true; - }; - - networking.hostName = "''${hostname}"; - greg = { - home = true; - tailscale.enable = true; - }; - } - EOF - cp /etc/nixos.bk/hardware-configuration.nix "/etc/nixos/hosts/''${hostname}/hardware-configuration.nix" - - # Prepare home-manager portion for setup - mkdir -p "/etc/nixos/home/hosts/''${hostname}" - cat << EOF > "/etc/nixos/home/hosts/''${hostname}/default.nix" - { pkgs, config, ... }: - - { - } - EOF - - # Prepares it for injecting the use case into the flake usage - cp /etc/nixos.bk/hardware-configuration.nix /etc/nixos - chown -R greg nixos - - echo "Now you should be able to just run `nixos-rebuild switch` to enable the flake functionality" - echo "After that and adding the entry to the flake, run `nixos-rebuild boot --flake '.#''${hostname}'` and reboot" -'' diff --git a/overlays/setup-ssh/default.nix b/overlays/setup-ssh/default.nix deleted file mode 100644 index 2ea5e6f..0000000 --- a/overlays/setup-ssh/default.nix +++ /dev/null @@ -1,3 +0,0 @@ -{ pkgs, gh, ... }: - -pkgs.writeShellScriptBin "setup-ssh" (builtins.replaceStrings [ "gh " ] [ "${gh}/bin/gh " ] (builtins.readFile ./setup-ssh.sh)) diff --git a/overlays/upgrade-pg-cluster.nix b/overlays/upgrade-pg-cluster.nix deleted file mode 100644 index 90fd818..0000000 --- a/overlays/upgrade-pg-cluster.nix +++ /dev/null @@ -1,25 +0,0 @@ -{ postgresql_15, postgresql_16, writeScriptBin, ... }: - -let - newPostgres = postgresql_16; - oldPostgres = postgresql_15; -in -writeScriptBin "upgrade-pg-cluster" '' - set -eux - systemctl stop postgresql - - export NEWDATA="/var/lib/postgresql/${newPostgres.psqlSchema}" - export NEWBIN="${newPostgres}/bin" - - export OLDDATA="/var/lib/postgresql/${oldPostgres.psqlSchema}" - export OLDBIN="${oldPostgres}/bin" - - install -d -m 0700 -o postgres -g postgres "$NEWDATA" - cd "$NEWDATA" - sudo -u postgres "$NEWBIN/initdb" -D "$NEWDATA" - - sudo -u postgres "$NEWBIN/pg_upgrade" \ - --old-datadir "$OLDDATA" --new-datadir "$NEWDATA" \ - --old-bindir "$OLDBIN" --new-bindir "$NEWBIN" \ - "$@" -'' diff --git a/pkgs/aacs.nix b/pkgs/aacs.nix new file mode 100644 index 0000000..2d9a7f9 --- /dev/null +++ b/pkgs/aacs.nix @@ -0,0 +1,14 @@ +{ writeShellApplication, unzip, ... }: + +writeShellApplication { + name = "aacs"; + runtimeInputs = [ unzip ]; + text = '' + [ ! -d "''${HOME}/.config/aacs" ] && mkdir -p "''${HOME}/.config/aacs" + cd "''${HOME}/.config/aacs" + [ -f KEYDB.cfg.zip ] && rm -f KEYDB.cfg.zip + curl -L -o KEYDB.cfg.zip "http://fvonline-db.bplaced.net/fv_download.php?lang=eng" + unzip KEYDB.cfg.zip + mv keydb.cfg KEYDB.config + ''; +} diff --git a/pkgs/create_ssl.nix b/pkgs/create_ssl.nix new file mode 100644 index 0000000..3fba59d --- /dev/null +++ b/pkgs/create_ssl.nix @@ -0,0 +1,32 @@ +{ writeShellApplication, openssl, ... }: + +writeShellApplication { + name = "create_ssl"; + runtimeInputs = [ openssl ]; + text = '' + set -e -o pipefail + name="''${1}" + root_key="''${2}" + + function usage { + echo "USAGE: create_ssl " + } + + if [ -z "''${name}" ]; then + usage + exit 1 + fi + + if [ -z "''${root_key}" ]; then + usage + exit 1 + fi + + # Create the certificate key + openssl ecparam -out "''${name}.key" -name prime256v1 -genkey + # Create the CSR + openssl req -name -sha256 -key "''${name}.key" -out "''${name}.csr" + # Sign it + openssl x509 -req -in "''${name}.csr" -CA "''${root_key}.crt" -CAkey "''${root_key}.key" -CAcreateserial -out "''${name}.crt" -days 3650 -sha256 + ''; +} diff --git a/pkgs/default.nix b/pkgs/default.nix index 5d4bd6a..ba581c7 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -8,6 +8,12 @@ in #default = iso; #iso = top.self.nixosConfigurations.iso.config.system.build.isoImage; #iso-beta = self.nixosConfigurations.iso-beta.config.system.build.isoImage; + aacs = c ./aacs.nix { }; + create_ssl = c ./create_ssl.nix { }; + inject-darwin = c ./inject-darwin.nix { }; + inject = c ./inject.nix { }; hms = c ./hms { }; + setup-ssh = c ./setup-ssh { }; + upgrade-pg-cluster = c ./upgrade-pg-cluster.nix { }; }; } diff --git a/pkgs/inject-darwin.nix b/pkgs/inject-darwin.nix new file mode 100644 index 0000000..6623bb3 --- /dev/null +++ b/pkgs/inject-darwin.nix @@ -0,0 +1,38 @@ +{ + writeShellApplication, + coreutils, + curl, + gnutar, + nix, + ... +}: +writeShellApplication { + name = "inject-darwin"; + runtimeInputs = [ + coreutils + curl + gnutar + nix + ]; + text = '' + dir="$(mktemp -d)" + cd "''${dir}" + + # Install nix-darwin + nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A installer + ./result/bin/darwin-installer + + # Get my configuration + mkdir -p ~/.config/darwin + cd ~/.config/darwin + curl -O -L https://github.com/greg-hellings/nixos-config/archive/refs/heads/main.tar.gz + tar xvzf main.tar.gz --strip-components 1 + + # Build NixOS for this system + pushd "''${dir}" + nix build "~/.config/darwin#darwinConfigurations.$(hostname -s).system" + ./result/sw/bin/darwin-rebuild switch --flake ~/.config/darwin + popd + rm -r "''${dir}" + ''; +} diff --git a/pkgs/inject.nix b/pkgs/inject.nix new file mode 100644 index 0000000..793dd4d --- /dev/null +++ b/pkgs/inject.nix @@ -0,0 +1,56 @@ +{ writeShellApplication, git, ... }: + +writeShellApplication { + name = "inject-nixos-config"; + runtimeInputs = [ git ]; + text = '' + hostname="''${1}" + if [ -n "''${hostname}"]; then + echo "You must provide a hostname"; + exit 1; + fi + + mv /etc/nixos /etc/nixos.bk + cd /etc + git clone http://github.com/greg-hellings/nixos-config nixos + mkdir -p "/etc/nixos/hosts/''${hostname}" + + # Prepares everything for the flake usage + #cp /etc/nixos.bk/configuration.nix "/etc/nixos/hosts/''${hostname}/default.nix" + cat << EOF > "/etc/nixos/hosts/''${hostname}/default.nix" + { pkgs, config, ... }: + + { + imports = [ ./hardware-configuration.nix ]; + + boot.loader = { + systemd-boot.enable = true; + efi.canTouchEfiVariables = true; + }; + + networking.hostName = "''${hostname}"; + greg = { + home = true; + tailscale.enable = true; + }; + } + EOF + cp /etc/nixos.bk/hardware-configuration.nix "/etc/nixos/hosts/''${hostname}/hardware-configuration.nix" + + # Prepare home-manager portion for setup + mkdir -p "/etc/nixos/home/hosts/''${hostname}" + cat << EOF > "/etc/nixos/home/hosts/''${hostname}/default.nix" + { pkgs, config, ... }: + + { + } + EOF + + # Prepares it for injecting the use case into the flake usage + cp /etc/nixos.bk/hardware-configuration.nix /etc/nixos + chown -R greg nixos + + echo "Now you should be able to just run `nixos-rebuild switch` to enable the flake functionality" + echo "After that and adding the entry to the flake, run `nixos-rebuild boot --flake '.#''${hostname}'` and reboot" + ''; +} diff --git a/pkgs/setup-ssh/default.nix b/pkgs/setup-ssh/default.nix new file mode 100644 index 0000000..dcfe7fc --- /dev/null +++ b/pkgs/setup-ssh/default.nix @@ -0,0 +1,7 @@ +{ writeShellApplication, gh, ... }: + +writeShellApplication { + name = "setup-ssh"; + runtimeInputs = [ gh ]; + text = builtins.readFile ./setup-ssh.sh; +} diff --git a/overlays/setup-ssh/setup-ssh.sh b/pkgs/setup-ssh/setup-ssh.sh similarity index 84% rename from overlays/setup-ssh/setup-ssh.sh rename to pkgs/setup-ssh/setup-ssh.sh index 90e465b..9c3002a 100644 --- a/overlays/setup-ssh/setup-ssh.sh +++ b/pkgs/setup-ssh/setup-ssh.sh @@ -7,5 +7,5 @@ if [ ! -f "${file}" ]; then echo "Authing key to GitHub - may fail" gh auth refresh -h github.com -s admin:public_key - gh ssh-key add -t ${HOSTNAME}-auto "${file}" || true + gh ssh-key add -t "${HOSTNAME}-auto" "${file}" || true fi diff --git a/pkgs/upgrade-pg-cluster.nix b/pkgs/upgrade-pg-cluster.nix new file mode 100644 index 0000000..ffc2a50 --- /dev/null +++ b/pkgs/upgrade-pg-cluster.nix @@ -0,0 +1,36 @@ +{ + writeShellApplication, + postgresql_15, + postgresql_16, + ... +}: + +let + newPostgres = postgresql_16; + oldPostgres = postgresql_15; +in +writeShellApplication { + name = "upgrade-pg-cluster"; + # Only the new one should be in scope, because that is the + # one that we will be referencing directly with the sudo command + # down farther. + runtimeInputs = [ newPostgres ]; + text = '' + systemctl stop postgresql + + export NEWDATA="/var/lib/postgresql/${newPostgres.psqlSchema}" + export NEWBIN="${newPostgres}/bin" + + export OLDDATA="/var/lib/postgresql/${oldPostgres.psqlSchema}" + export OLDBIN="${oldPostgres}/bin" + + install -d -m 0700 -o postgres -g postgres "$NEWDATA" + cd "$NEWDATA" + sudo -u postgres "$NEWBIN/initdb" -D "$NEWDATA" + + sudo -u postgres "$NEWBIN/pg_upgrade" \ + --old-datadir "$OLDDATA" --new-datadir "$NEWDATA" \ + --old-bindir "$OLDBIN" --new-bindir "$NEWBIN" \ + "$@" + ''; +}