{ lib, config, pkgs, ... }: let # Copy your script to the Nix store userScript = pkgs.runCommand "start_script.sh" {} '' cp ${./assets/scripts/start_script.sh} $out chmod +x $out ''; # Create a wrapper script that runs in the user's home directory script = pkgs.writeShellScriptBin "startScript" '' #!${pkgs.bash}/bin/bash set -euo pipefail # Get the home directory of the user who ran 'nixos-rebuild switch' (usually root, so use SUDO_USER) USER_HOME=$(getent passwd "$SUDO_USER" | cut -d: -f6) if [ -z "$USER_HOME" ]; then # Fallback: use the first user in /home USER_HOME=$(ls -d /home/* | head -n 1) fi cd "$USER_HOME" ${userScript} ''; in { system.activationScripts.startScript = lib.mkIf (builtins.pathExists ./assets/scripts/start_script.sh) { text = '' ${script}/bin/startScript ''; }; }