Skip to content
Closed
Show file tree
Hide file tree
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
152 changes: 52 additions & 100 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,34 +90,14 @@ else ifdef CI
NIX_FLAGS += --option substituters "$(NIX_SUBSTITUTERS)" --option trusted-public-keys "$(NIX_TRUSTED_KEYS)"
endif

# Machine detection for automatic host mapping
DETECTED_HOST := $(shell \
if [ "$(OS)" = "Darwin" ] && [ "$(shell whoami)" = "shunkakinoki" ] && [ "$(ARCH)" = "arm64" ]; then \
computer_name=$$(scutil --get ComputerName 2>/dev/null || echo ""); \
if echo "$$computer_name" | grep -q "Shun's MacBook M4"; then \
echo "galactica"; \
else \
echo ""; \
fi; \
elif [ "$(OS)" = "Linux" ]; then \
if [ -n "$$RUNPOD_POD_ID" ]; then \
echo "pod"; \
else \
hostname=$$(hostname 2>/dev/null || echo ""); \
if [ "$$hostname" = "kyber" ]; then \
echo "kyber"; \
elif [ "$$hostname" = "matic" ]; then \
echo "matic"; \
elif [ "$(DMI_SYS_VENDOR)" = "Framework" ] \
&& echo "$(DMI_PRODUCT_NAME)" | grep -q "Laptop 13.*AMD Ryzen AI 300"; then \
echo "matic"; \
else \
echo ""; \
fi; \
fi; \
else \
echo ""; \
fi)
# Machine detection for automatic host mapping. The logic lives in a script so
# it can be exercised directly by spec/detect_host_spec.sh.
DETECTED_HOST := $(shell OS='$(OS)' ARCH='$(ARCH)' \
DMI_SYS_VENDOR='$(DMI_SYS_VENDOR)' DMI_PRODUCT_NAME='$(DMI_PRODUCT_NAME)' \
bash ./scripts/detect-host.sh)
Comment on lines +95 to +97

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.

high

Passing variables inline to $(shell ...) using single quotes can break the shell syntax if any of the variables (especially DMI_PRODUCT_NAME or DMI_SYS_VENDOR which are read from system files) contain single quotes (e.g., Shun's Laptop).

Instead of passing them inline, you can use the export directive in the Makefile. This makes the variables automatically available in the environment of the $(shell ...) subshell, eliminating any quoting or escaping issues entirely.

export OS ARCH DMI_SYS_VENDOR DMI_PRODUCT_NAME
DETECTED_HOST := $(shell bash ./scripts/detect-host.sh)


# Effective host: an explicit HOST always wins over machine detection.
RESOLVED_HOST := $(or $(HOST),$(DETECTED_HOST))

# User's home directory
HOME_DIR := $(shell echo $$HOME)
Expand Down Expand Up @@ -161,6 +141,11 @@ install: setup git-submodule-sync nix-build nix-switch shell-install ## Set up f
.PHONY: build
build: nix-build ## Build Nix configuration.

.PHONY: detect-host
detect-host: ## Show the host this machine maps to (and the host builds will use).
@echo "Detected host: $(if $(DETECTED_HOST),$(DETECTED_HOST),<none>)"
@echo "Resolved host: $(if $(RESOLVED_HOST),$(RESOLVED_HOST),<none>)$(if $(HOST), (from HOST=$(HOST)),)"

.PHONY: check
check: ## Run all validation checks (nix, format, lua).
@echo "🔍 Running all validation checks..."
Expand Down Expand Up @@ -526,40 +511,31 @@ nix-build: nix-connect nix-trust ## Build Nix configuration.
@if [ "$$CI" = "true" ] || [ "$$IN_DOCKER" = "true" ]; then \
echo "🤖 Running in CI/Docker environment"; \
if [ "$(OS)" = "Darwin" ]; then \
if [ -n "$(HOST)" ]; then \
if [ -n "$(RESOLVED_HOST)" ]; then \
case " $(NIXOS_NAMED_HOSTS) " in \
*" $(HOST) "*) \
echo "Building named NixOS host: $(HOST)"; \
HOST=$(HOST) HOSTNAME=$(HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#nixosConfigurations.$(HOST).config.system.build.toplevel $(NIX_FLAGS) --impure --no-update-lock-file --show-trace; \
*" $(RESOLVED_HOST) "*) \
echo "Building named NixOS host: $(RESOLVED_HOST)"; \
HOST=$(RESOLVED_HOST) HOSTNAME=$(RESOLVED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#nixosConfigurations.$(RESOLVED_HOST).config.system.build.toplevel $(NIX_FLAGS) --impure --no-update-lock-file --show-trace; \
;; \
*) \
echo "Building named Darwin host: $(HOST)"; \
HOST=$(HOST) HOSTNAME=$(HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#darwinConfigurations.$(HOST).system $(NIX_FLAGS) --impure --no-update-lock-file --show-trace; \
echo "Building named Darwin host: $(RESOLVED_HOST)"; \
HOST=$(RESOLVED_HOST) HOSTNAME=$(RESOLVED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#darwinConfigurations.$(RESOLVED_HOST).system $(NIX_FLAGS) --impure --no-update-lock-file --show-trace; \
;; \
esac; \
elif [ -n "$(DETECTED_HOST)" ]; then \
echo "Auto-detected host: $(DETECTED_HOST)"; \
HOST=$(DETECTED_HOST) HOSTNAME=$(DETECTED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#darwinConfigurations.$(DETECTED_HOST).system $(NIX_FLAGS) --impure --no-update-lock-file --show-trace; \
else \
HOST=runner HOSTNAME=runner $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#$(NIX_CONFIG_TYPE).runner.system $(NIX_FLAGS) --impure --no-update-lock-file --show-trace; \
fi; \
elif [ "$(NIX_CONFIG_TYPE)" = "nixosConfigurations" ]; then \
if [ -n "$(HOST)" ]; then \
echo "Building named NixOS host: $(HOST)"; \
$(SUDO) env HOST=$(HOST) HOSTNAME=$(HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) nixpkgs#nixos-rebuild -- build --flake .#$(HOST) --impure; \
elif [ -n "$(DETECTED_HOST)" ]; then \
echo "Auto-detected host: $(DETECTED_HOST)"; \
$(SUDO) env HOST=$(DETECTED_HOST) HOSTNAME=$(DETECTED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) nixpkgs#nixos-rebuild -- build --flake .#$(DETECTED_HOST) --impure; \
if [ -n "$(RESOLVED_HOST)" ]; then \
echo "Building named NixOS host: $(RESOLVED_HOST)"; \
$(SUDO) env HOST=$(RESOLVED_HOST) HOSTNAME=$(RESOLVED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) nixpkgs#nixos-rebuild -- build --flake .#$(RESOLVED_HOST) --impure; \
else \
HOST=runner HOSTNAME=runner $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#nixosConfigurations.runner.config.system.build.toplevel $(NIX_FLAGS) --impure --no-update-lock-file --show-trace; \
fi; \
elif [ "$(NIX_CONFIG_TYPE)" = "homeConfigurations" ]; then \
if [ -n "$(HOST)" ]; then \
echo "Building named home config: $(HOST)"; \
HOST=$(HOST) HOSTNAME=$(HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#homeConfigurations.$(HOST).activationPackage $(NIX_FLAGS) --impure --no-update-lock-file --show-trace; \
elif [ -n "$(DETECTED_HOST)" ]; then \
echo "Auto-detected host: $(DETECTED_HOST)"; \
HOST=$(DETECTED_HOST) HOSTNAME=$(DETECTED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#homeConfigurations.$(DETECTED_HOST).activationPackage $(NIX_FLAGS) --impure --no-update-lock-file --show-trace; \
if [ -n "$(RESOLVED_HOST)" ]; then \
echo "Building named home config: $(RESOLVED_HOST)"; \
HOST=$(RESOLVED_HOST) HOSTNAME=$(RESOLVED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#homeConfigurations.$(RESOLVED_HOST).activationPackage $(NIX_FLAGS) --impure --no-update-lock-file --show-trace; \
else \
HOST=$(NIX_SYSTEM) HOSTNAME=$(NIX_SYSTEM) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#$(NIX_CONFIG_TYPE)."$(NIX_USERNAME)@$(NIX_SYSTEM)".activationPackage $(NIX_FLAGS) --impure --no-update-lock-file --show-trace; \
fi; \
Expand All @@ -572,40 +548,31 @@ nix-build: nix-connect nix-trust ## Build Nix configuration.
echo "❌ Unsupported system architecture: $(OS) $(ARCH)"; \
exit 1; \
elif [ "$(OS)" = "Darwin" ]; then \
if [ -n "$(HOST)" ]; then \
if [ -n "$(RESOLVED_HOST)" ]; then \
case " $(NIXOS_NAMED_HOSTS) " in \
*" $(HOST) "*) \
echo "Building named NixOS host: $(HOST)"; \
HOST=$(HOST) HOSTNAME=$(HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#nixosConfigurations.$(HOST).config.system.build.toplevel $(NIX_FLAGS) --impure --show-trace; \
*" $(RESOLVED_HOST) "*) \
echo "Building named NixOS host: $(RESOLVED_HOST)"; \
HOST=$(RESOLVED_HOST) HOSTNAME=$(RESOLVED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#nixosConfigurations.$(RESOLVED_HOST).config.system.build.toplevel $(NIX_FLAGS) --impure --show-trace; \
;; \
*) \
echo "Building named Darwin host: $(HOST)"; \
HOST=$(HOST) HOSTNAME=$(HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#darwinConfigurations.$(HOST).system $(NIX_FLAGS) --impure --show-trace; \
echo "Building named Darwin host: $(RESOLVED_HOST)"; \
HOST=$(RESOLVED_HOST) HOSTNAME=$(RESOLVED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#darwinConfigurations.$(RESOLVED_HOST).system $(NIX_FLAGS) --impure --show-trace; \
;; \
esac; \
elif [ -n "$(DETECTED_HOST)" ]; then \
echo "Auto-detected host: $(DETECTED_HOST)"; \
HOST=$(DETECTED_HOST) HOSTNAME=$(DETECTED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#darwinConfigurations.$(DETECTED_HOST).system $(NIX_FLAGS) --impure --show-trace; \
else \
HOST=$(NIX_SYSTEM) HOSTNAME=$(NIX_SYSTEM) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#$(NIX_CONFIG_TYPE).$(NIX_SYSTEM).system $(NIX_FLAGS) --impure --show-trace; \
fi; \
elif [ "$(NIX_CONFIG_TYPE)" = "nixosConfigurations" ]; then \
if [ -n "$(HOST)" ]; then \
echo "Building named host: $(HOST)"; \
$(SUDO) env HOST=$(HOST) HOSTNAME=$(HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) nixpkgs#nixos-rebuild -- build --flake .#$(HOST) --impure; \
elif [ -n "$(DETECTED_HOST)" ]; then \
echo "Auto-detected host: $(DETECTED_HOST)"; \
$(SUDO) env HOST=$(DETECTED_HOST) HOSTNAME=$(DETECTED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) nixpkgs#nixos-rebuild -- build --flake .#$(DETECTED_HOST) --impure; \
if [ -n "$(RESOLVED_HOST)" ]; then \
echo "Building named host: $(RESOLVED_HOST)"; \
$(SUDO) env HOST=$(RESOLVED_HOST) HOSTNAME=$(RESOLVED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) nixpkgs#nixos-rebuild -- build --flake .#$(RESOLVED_HOST) --impure; \
else \
$(SUDO) env HOST=$(NIX_SYSTEM) HOSTNAME=$(NIX_SYSTEM) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) nixpkgs#nixos-rebuild -- build --flake .#$(NIX_SYSTEM) --impure; \
fi; \
elif [ "$(NIX_CONFIG_TYPE)" = "homeConfigurations" ]; then \
if [ -n "$(HOST)" ]; then \
echo "Building named home config: $(HOST)"; \
HOST=$(HOST) HOSTNAME=$(HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#homeConfigurations.$(HOST).activationPackage $(NIX_FLAGS) --impure --show-trace; \
elif [ -n "$(DETECTED_HOST)" ]; then \
echo "Auto-detected host: $(DETECTED_HOST)"; \
HOST=$(DETECTED_HOST) HOSTNAME=$(DETECTED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#homeConfigurations.$(DETECTED_HOST).activationPackage $(NIX_FLAGS) --impure --show-trace; \
if [ -n "$(RESOLVED_HOST)" ]; then \
echo "Building named home config: $(RESOLVED_HOST)"; \
HOST=$(RESOLVED_HOST) HOSTNAME=$(RESOLVED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#homeConfigurations.$(RESOLVED_HOST).activationPackage $(NIX_FLAGS) --impure --show-trace; \
else \
HOST=$(NIX_SYSTEM) HOSTNAME=$(NIX_SYSTEM) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) build .#$(NIX_CONFIG_TYPE)."$(NIX_USERNAME)@$(NIX_SYSTEM)".activationPackage $(NIX_FLAGS) --impure --show-trace; \
fi; \
Expand Down Expand Up @@ -697,25 +664,19 @@ nix-switch: ## Activate Nix configuration.
if [ "$(OS)" = "Darwin" ]; then \
$(SUDO) env CI="$$CI" IN_DOCKER="$$IN_DOCKER" HOST=runner HOSTNAME=runner $(NIX_ALLOW_UNFREE) $(DARWIN_REBUILD) switch --flake .#runner --impure --no-update-lock-file; \
elif [ "$(NIX_CONFIG_TYPE)" = "nixosConfigurations" ]; then \
if [ -n "$(HOST)" ]; then \
echo "Switching named host: $(HOST)"; \
$(SUDO) env HOST=$(HOST) HOSTNAME=$(HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) --impure nixpkgs#nixos-rebuild -- switch --flake .#$(HOST) --no-update-lock-file || exit 0; \
elif [ -n "$(DETECTED_HOST)" ]; then \
echo "Auto-detected host: $(DETECTED_HOST)"; \
$(SUDO) env HOST=$(DETECTED_HOST) HOSTNAME=$(DETECTED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) --impure nixpkgs#nixos-rebuild -- switch --flake .#$(DETECTED_HOST) --no-update-lock-file || exit 0; \
if [ -n "$(RESOLVED_HOST)" ]; then \
echo "Switching named host: $(RESOLVED_HOST)"; \
$(SUDO) env HOST=$(RESOLVED_HOST) HOSTNAME=$(RESOLVED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) --impure nixpkgs#nixos-rebuild -- switch --flake .#$(RESOLVED_HOST) --no-update-lock-file || exit 0; \
else \
echo "⏭️ NixOS switch skipped in CI as the runner is not a NixOS system"; \
$(SUDO) env HOST=runner HOSTNAME=runner $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) --impure nixpkgs#nixos-rebuild -- switch --flake .#runner --no-update-lock-file || exit 0; \
fi; \
elif [ "$(NIX_CONFIG_TYPE)" = "homeConfigurations" ]; then \
if [ "$$SKIP_HOME_MANAGER_SWITCH" = "true" ]; then \
echo "⏭️ Home-manager switch skipped (SKIP_HOME_MANAGER_SWITCH=true)"; \
elif [ -n "$(HOST)" ]; then \
echo "Switching named home config: $(HOST)"; \
HOST=$(HOST) HOSTNAME=$(HOST) USER=$(NIX_USERNAME) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) --impure .#homeConfigurations.$(HOST).activationPackage; \
elif [ -n "$(DETECTED_HOST)" ]; then \
echo "Auto-detected host: $(DETECTED_HOST)"; \
HOST=$(DETECTED_HOST) HOSTNAME=$(DETECTED_HOST) USER=$(NIX_USERNAME) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) --impure .#homeConfigurations.$(DETECTED_HOST).activationPackage; \
elif [ -n "$(RESOLVED_HOST)" ]; then \
echo "Switching named home config: $(RESOLVED_HOST)"; \
HOST=$(RESOLVED_HOST) HOSTNAME=$(RESOLVED_HOST) USER=$(NIX_USERNAME) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) --impure .#homeConfigurations.$(RESOLVED_HOST).activationPackage; \
else \
HOST=$(NIX_SYSTEM) HOSTNAME=$(NIX_SYSTEM) USER=$(NIX_USERNAME) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) --impure .#$(NIX_CONFIG_TYPE)."$(NIX_USERNAME)@$(NIX_SYSTEM)".activationPackage; \
fi; \
Expand All @@ -728,32 +689,23 @@ nix-switch: ## Activate Nix configuration.
echo "❌ Unsupported system architecture: $(OS) $(ARCH)"; \
exit 1; \
elif [ "$(OS)" = "Darwin" ]; then \
if [ -n "$(HOST)" ]; then \
echo "Switching named host: $(HOST)"; \
$(SUDO) env HOST=$(HOST) HOSTNAME=$(HOST) $(NIX_ALLOW_UNFREE) $(DARWIN_REBUILD) switch --flake .#$(HOST) --impure; \
elif [ -n "$(DETECTED_HOST)" ]; then \
echo "Auto-detected host: $(DETECTED_HOST)"; \
$(SUDO) env HOST=$(DETECTED_HOST) HOSTNAME=$(DETECTED_HOST) $(NIX_ALLOW_UNFREE) $(DARWIN_REBUILD) switch --flake .#$(DETECTED_HOST) --impure; \
if [ -n "$(RESOLVED_HOST)" ]; then \
echo "Switching named host: $(RESOLVED_HOST)"; \
$(SUDO) env HOST=$(RESOLVED_HOST) HOSTNAME=$(RESOLVED_HOST) $(NIX_ALLOW_UNFREE) $(DARWIN_REBUILD) switch --flake .#$(RESOLVED_HOST) --impure; \
else \
$(SUDO) env HOST=$(NIX_SYSTEM) HOSTNAME=$(NIX_SYSTEM) $(NIX_ALLOW_UNFREE) $(DARWIN_REBUILD) switch --flake .#$(NIX_SYSTEM) --impure; \
fi; \
elif [ "$(NIX_CONFIG_TYPE)" = "nixosConfigurations" ]; then \
if [ -n "$(HOST)" ]; then \
echo "Switching named host: $(HOST)"; \
$(SUDO) env HOST=$(HOST) HOSTNAME=$(HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) nixpkgs#nixos-rebuild -- switch --flake .#$(HOST) --impure; \
elif [ -n "$(DETECTED_HOST)" ]; then \
echo "Auto-detected host: $(DETECTED_HOST)"; \
$(SUDO) env HOST=$(DETECTED_HOST) HOSTNAME=$(DETECTED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) nixpkgs#nixos-rebuild -- switch --flake .#$(DETECTED_HOST) --impure; \
if [ -n "$(RESOLVED_HOST)" ]; then \
echo "Switching named host: $(RESOLVED_HOST)"; \
$(SUDO) env HOST=$(RESOLVED_HOST) HOSTNAME=$(RESOLVED_HOST) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) nixpkgs#nixos-rebuild -- switch --flake .#$(RESOLVED_HOST) --impure; \
else \
$(SUDO) env HOST=$(NIX_SYSTEM) HOSTNAME=$(NIX_SYSTEM) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) nixpkgs#nixos-rebuild -- switch --flake .#$(NIX_SYSTEM) --impure; \
fi; \
elif [ "$(NIX_CONFIG_TYPE)" = "homeConfigurations" ]; then \
if [ -n "$(HOST)" ]; then \
echo "Switching named home config: $(HOST)"; \
HOST=$(HOST) HOSTNAME=$(HOST) USER=$(NIX_USERNAME) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) --impure .#homeConfigurations.$(HOST).activationPackage; \
elif [ -n "$(DETECTED_HOST)" ]; then \
echo "Auto-detected host: $(DETECTED_HOST)"; \
HOST=$(DETECTED_HOST) HOSTNAME=$(DETECTED_HOST) USER=$(NIX_USERNAME) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) --impure .#homeConfigurations.$(DETECTED_HOST).activationPackage; \
if [ -n "$(RESOLVED_HOST)" ]; then \
echo "Switching named home config: $(RESOLVED_HOST)"; \
HOST=$(RESOLVED_HOST) HOSTNAME=$(RESOLVED_HOST) USER=$(NIX_USERNAME) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) --impure .#homeConfigurations.$(RESOLVED_HOST).activationPackage; \
else \
HOST=$(NIX_SYSTEM) HOSTNAME=$(NIX_SYSTEM) USER=$(NIX_USERNAME) $(NIX_ALLOW_UNFREE) $(NIX_EXEC) run $(NIX_FLAGS) --impure .#$(NIX_CONFIG_TYPE)."$(NIX_USERNAME)@$(NIX_SYSTEM)".activationPackage; \
fi; \
Expand Down
60 changes: 60 additions & 0 deletions scripts/detect-host.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env bash
# Resolve the named host configuration for the current machine.
#
# Prints the host name on stdout, or nothing when the machine does not map to
# a named host. Reads its inputs from the environment so callers (and specs)
# can drive detection without touching the real machine:
#
# OS, ARCH uname-derived platform
# DMI_SYS_VENDOR, DMI_PRODUCT_NAME /sys/class/dmi/id values on Linux
# RUNPOD_POD_ID set inside RunPod containers

set -euo pipefail

OS="${OS:-}"
ARCH="${ARCH:-}"
DMI_SYS_VENDOR="${DMI_SYS_VENDOR:-}"
DMI_PRODUCT_NAME="${DMI_PRODUCT_NAME:-}"
RUNPOD_POD_ID="${RUNPOD_POD_ID:-}"

detect_darwin_host() {
if [ "$(whoami 2>/dev/null || true)" != "shunkakinoki" ] || [ "$ARCH" != "arm64" ]; then
return 0
fi

local computer_name
computer_name="$(scutil --get ComputerName 2>/dev/null || true)"
if echo "$computer_name" | grep -q "Shun's MacBook M4"; then

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

Using echo to output arbitrary system-provided variables (like computer_name) can be problematic if the value starts with a hyphen (which echo might interpret as an option) or contains backslashes. Using printf '%s\n' is the standard, robust way to output variable values safely.

Suggested change
if echo "$computer_name" | grep -q "Shun's MacBook M4"; then
if printf '%s\n' "$computer_name" | grep -q "Shun's MacBook M4"; then

echo "galactica"
fi
return 0
}
Comment on lines +20 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Make the machine probes injectable for deterministic detection tests.

The script still reads whoami, scutil, and hostname directly. Consequently, the Linux specs expecting DMI or empty output can fail on a machine named kyber or matic, while the positive Darwin mapping cannot be isolated in tests.

Accept environment overrides for these values, falling back to the commands only when the overrides are unset; then explicitly neutralize/set them in spec/detect_host_spec.sh.

Also applies to: 39-46

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@scripts/detect-host.sh` around lines 20 - 31, Update the machine-probe logic
used by detect_darwin_host and the related hostname detection to accept
environment overrides for whoami, scutil ComputerName, and hostname, falling
back to the existing commands only when each override is unset. Update
spec/detect_host_spec.sh to explicitly neutralize or set these overrides for
deterministic Linux and Darwin detection cases.


detect_linux_host() {
if [ -n "$RUNPOD_POD_ID" ]; then
echo "pod"
return 0
fi

local hostname
hostname="$(hostname 2>/dev/null || true)"
case "$hostname" in
kyber | matic)
echo "$hostname"
return 0
;;
esac

# Framework 13 (AMD Ryzen AI 300) ships with a generic hostname, so fall back
# to the DMI identity burned into the board.
if [ "$DMI_SYS_VENDOR" = "Framework" ] &&
echo "$DMI_PRODUCT_NAME" | grep -q "Laptop 13.*AMD Ryzen AI 300"; then
Comment on lines +50 to +51

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

Using echo to output arbitrary system-provided variables (like DMI_PRODUCT_NAME) can be problematic if the value starts with a hyphen or contains backslashes. Using printf '%s\n' is the standard, robust way to output variable values safely.

Suggested change
if [ "$DMI_SYS_VENDOR" = "Framework" ] &&
echo "$DMI_PRODUCT_NAME" | grep -q "Laptop 13.*AMD Ryzen AI 300"; then
if [ "$DMI_SYS_VENDOR" = "Framework" ] &&
printf '%s\n' "$DMI_PRODUCT_NAME" | grep -q "Laptop 13.*AMD Ryzen AI 300"; then

echo "matic"
fi
return 0
}

case "$OS" in
Darwin) detect_darwin_host ;;
Linux) detect_linux_host ;;
esac
81 changes: 81 additions & 0 deletions spec/detect_host_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env bash
# shellcheck disable=SC2329

Describe 'scripts/detect-host.sh'
SCRIPT="$PWD/scripts/detect-host.sh"

Describe 'script properties'
It 'uses bash shebang'
When run bash -c "head -1 '$SCRIPT'"
The output should include '#!/usr/bin/env bash'
End

It 'uses strict mode'
When run bash -c "grep 'set -euo pipefail' '$SCRIPT'"
The output should include 'set -euo pipefail'
End
End

Describe 'Linux detection'
It 'maps a Framework 13 AMD Ryzen AI 300 board to matic'
When run env OS=Linux ARCH=x86_64 DMI_SYS_VENDOR=Framework \
DMI_PRODUCT_NAME='Laptop 13 (AMD Ryzen AI 300 Series)' \
RUNPOD_POD_ID= bash "$SCRIPT"
The status should be success
The output should equal 'matic'
End

It 'prefers a RunPod pod over DMI data'
When run env OS=Linux ARCH=x86_64 DMI_SYS_VENDOR=Framework \
DMI_PRODUCT_NAME='Laptop 13 (AMD Ryzen AI 300 Series)' \
RUNPOD_POD_ID=abc123 bash "$SCRIPT"
The status should be success
The output should equal 'pod'
End

It 'ignores a Framework board that is not the AMD Ryzen AI 300 model'
When run env OS=Linux ARCH=x86_64 DMI_SYS_VENDOR=Framework \
DMI_PRODUCT_NAME='Laptop 13 (12th Gen Intel Core)' \
RUNPOD_POD_ID= bash "$SCRIPT"
The status should be success
The output should equal ''
End

It 'ignores matching product data from another vendor'
When run env OS=Linux ARCH=x86_64 DMI_SYS_VENDOR=Acme \
DMI_PRODUCT_NAME='Laptop 13 (AMD Ryzen AI 300 Series)' \
RUNPOD_POD_ID= bash "$SCRIPT"
The status should be success
The output should equal ''
End

It 'reports no host when there is no DMI data'
When run env OS=Linux ARCH=x86_64 DMI_SYS_VENDOR= DMI_PRODUCT_NAME= \
RUNPOD_POD_ID= bash "$SCRIPT"
The status should be success
The output should equal ''
End
End

Describe 'Darwin detection'
It 'reports no host on a non-arm64 Mac'
When run env OS=Darwin ARCH=x86_64 bash "$SCRIPT"
The status should be success
The output should equal ''
End
End

Describe 'unknown platforms'
It 'reports no host'
When run env OS=Plan9 ARCH=x86_64 bash "$SCRIPT"
The status should be success
The output should equal ''
End

It 'reports no host when OS is unset'
When run env -u OS ARCH=x86_64 bash "$SCRIPT"
The status should be success
The output should equal ''
End
End
End
Loading