55 lines
1.7 KiB
Bash
Executable File
55 lines
1.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Unix-style user configuration - does one thing well
|
|
set -e
|
|
|
|
configure_user() {
|
|
local username="${1:-}"
|
|
local config_file="${2:-/etc/nixos/configuration.nix}"
|
|
local home_file="${3:-/etc/nixos/home.nix}"
|
|
|
|
# Get username if not provided
|
|
if [[ -z "$username" ]]; then
|
|
if [[ "${OMNIXY_QUIET:-}" == "1" ]]; then
|
|
echo "Username required in quiet mode" >&2
|
|
exit 1
|
|
fi
|
|
read -p "Username: " username
|
|
fi
|
|
|
|
# Validate username
|
|
if [[ ! "$username" =~ ^[a-zA-Z][a-zA-Z0-9_-]*$ ]]; then
|
|
echo "Invalid username: $username" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Update configuration files
|
|
if [[ -f "$config_file" ]]; then
|
|
sudo sed -i "s/user = \"user\"/user = \"$username\"/" "$config_file"
|
|
[[ "${OMNIXY_QUIET:-}" != "1" ]] && echo "Updated main configuration"
|
|
fi
|
|
|
|
if [[ -f "$home_file" ]]; then
|
|
sudo sed -i "s/home.username = \"user\"/home.username = \"$username\"/" "$home_file" 2>/dev/null || true
|
|
sudo sed -i "s|home.homeDirectory = \"/home/user\"|home.homeDirectory = \"/home/$username\"|" "$home_file" 2>/dev/null || true
|
|
[[ "${OMNIXY_QUIET:-}" != "1" ]] && echo "Updated home configuration"
|
|
fi
|
|
|
|
# Output username for scripting
|
|
echo "$username"
|
|
}
|
|
|
|
main() {
|
|
case "${1:-}" in
|
|
--help|-h)
|
|
echo "Usage: omnixy-configure-user [username] [config-file] [home-file]"
|
|
echo "Updates configuration files with specified username"
|
|
echo "Prompts for username if not provided (unless --quiet)"
|
|
exit 0
|
|
;;
|
|
*)
|
|
configure_user "$1" "$2" "$3"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
main "$@" |