Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions named-hosts/kyber/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,39 @@ home-manager.lib.homeManagerConfiguration {

# IP forwarding for Tailscale exit node
home.activation.enableIpForwarding = config.lib.dag.entryAfter [ "writeBoundary" ] ''
# Resolve an elevated command helper
SUDO_CMD=""
if command -v sudo >/dev/null 2>&1; then
SUDO_CMD="sudo"
elif [ -x /run/wrappers/bin/sudo ]; then
SUDO_CMD="/run/wrappers/bin/sudo"
elif [ -x /usr/bin/sudo ]; then
SUDO_CMD="/usr/bin/sudo"
elif command -v doas >/dev/null 2>&1; then
SUDO_CMD="doas"
elif [ -x /usr/bin/doas ]; then
SUDO_CMD="/usr/bin/doas"
elif [ "$(id -u)" -ne 0 ]; then
echo "IP forwarding requires root privileges, but sudo/doas is not available." >&2
exit 1
fi
Comment on lines +152 to +166

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for discovering an elevated command helper (sudo/doas) is quite verbose and duplicated from the tailscale module. Using a loop improves maintainability and readability. Additionally, to further resolve the PATH issues that prompted this PR, consider using absolute Nix store paths for other utilities like sysctl (e.g., ${pkgs.procps}/bin/sysctl) and tee (e.g., ${pkgs.coreutils}/bin/tee), as is done with rage and gnupg elsewhere in this file.

          SUDO_CMD=""
          for cmd in "sudo" "/run/wrappers/bin/sudo" "/usr/bin/sudo" "doas" "/usr/bin/doas"; do
            if command -v "$cmd" >/dev/null 2>&1 || [ -x "$cmd" ]; then
              SUDO_CMD="$cmd"
              break
            fi
          done

          if [ -z "$SUDO_CMD" ] && [ "$(id -u)" -ne 0 ]; then
            echo "IP forwarding requires root privileges, but sudo/doas is not available." >&2
            exit 1
          fi


run_root_cmd() {
if [ -n "$SUDO_CMD" ]; then
''${DRY_RUN_CMD:-} "$SUDO_CMD" "$@"
else
''${DRY_RUN_CMD:-} "$@"
Comment on lines +163 to +172
fi
}

if [ "$(cat /proc/sys/net/ipv4/ip_forward)" != "1" ]; then
echo "Enabling IP forwarding for Tailscale exit node..."
sudo sysctl -w net.ipv4.ip_forward=1
sudo sysctl -w net.ipv6.conf.all.forwarding=1
run_root_cmd sysctl -w net.ipv4.ip_forward=1
run_root_cmd sysctl -w net.ipv6.conf.all.forwarding=1
fi
Comment on lines 176 to 180
if [ ! -f /etc/sysctl.d/99-tailscale.conf ]; then
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-tailscale.conf
echo 'net.ipv6.conf.all.forwarding=1' | sudo tee -a /etc/sysctl.d/99-tailscale.conf
echo 'net.ipv4.ip_forward=1' | run_root_cmd tee /etc/sysctl.d/99-tailscale.conf
echo 'net.ipv6.conf.all.forwarding=1' | run_root_cmd tee -a /etc/sysctl.d/99-tailscale.conf
fi
'';

Expand Down
Loading