From 4e8afa2dfc6616beaac0f9acaf9c71dd118cb991 Mon Sep 17 00:00:00 2001 From: Nickolaj Jepsen Date: Tue, 20 Jan 2026 01:02:28 +0100 Subject: [PATCH] feat: add scripts --- hosts/default.nix | 1 + modules/scripts/default.nix | 25 +++++++++++++++++++++++++ modules/scripts/reboot-windows.bash | 27 +++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 modules/scripts/default.nix create mode 100644 modules/scripts/reboot-windows.bash diff --git a/hosts/default.nix b/hosts/default.nix index 403db58..fe6b1e2 100644 --- a/hosts/default.nix +++ b/hosts/default.nix @@ -36,6 +36,7 @@ ../modules/programs ../modules/desktop ../modules/homelab + ../modules/scripts host ] ++ modules; diff --git a/modules/scripts/default.nix b/modules/scripts/default.nix new file mode 100644 index 0000000..b22eea2 --- /dev/null +++ b/modules/scripts/default.nix @@ -0,0 +1,25 @@ +{ + pkgs, + lib, + ... +}: let + makeScript = { + path, + name ? lib.removeSuffix ".bash" (builtins.baseNameOf path), + runtimeInputs ? [], + }: + pkgs.writeShellApplication { + inherit name runtimeInputs; + text = builtins.readFile path; + }; +in { + environment.systemPackages = [ + (makeScript { + path = ./reboot-windows.bash; + runtimeInputs = with pkgs; [ + jq + systemd # for bootctl and systemctl + ]; + }) + ]; +} diff --git a/modules/scripts/reboot-windows.bash b/modules/scripts/reboot-windows.bash new file mode 100644 index 0000000..2044d45 --- /dev/null +++ b/modules/scripts/reboot-windows.bash @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +# Find windows entry id using bootctl +WINDOWS_ID=$(bootctl list --json=short | jq -r '.[] | select(.title != null) | select(.title | ascii_downcase | contains("windows")) | .id' | head -n 1) + +if [ -z "$WINDOWS_ID" ]; then + # Fallback to checking the ID itself + WINDOWS_ID=$(bootctl list --json=short | jq -r '.[] | select(.id | ascii_downcase | contains("windows")) | .id' | head -n 1) +fi + +if [ -z "$WINDOWS_ID" ]; then + echo "Error: No Windows boot entry found in systemd-boot." + exit 1 +fi + +echo "Setting next boot to: $WINDOWS_ID" + +# Check if we have root, if not, try to use sudo +if [ "$EUID" -ne 0 ]; then + sudo bootctl set-oneshot "$WINDOWS_ID" + echo "Rebooting..." + sudo systemctl reboot +else + bootctl set-oneshot "$WINDOWS_ID" + echo "Rebooting..." + systemctl reboot +fi