NIX_RUN := sudo nix run .\#
NIX_LOCK := nix flake lock

DEPS_MY := \
	myneovim \
	vnetod \
	yagpt_tg_bot

DEPS_NIXOS := \
	nixpkgs \
	nixpkgs-unstable \
	hardware \
	home-manager \
	agenix

DEPS_MISC := \
	wired \
	nil \
	mailserver

MACHINES := \
	home \
	asus-gl553vd

VPS := \
	istal \
	tatos

.PHONY: help
help:
	bat -p Makefile

define machine_rule
.PHONY: $(1)
$(1): ;
	systemctl --user reset-failed
	$$(NIX_RUN)switch/$(1)

.PHONY: rollback-$(1)
rollback-$(1):
	$$(NIX_RUN)rollback/$(1)

.PHONY: test-$(1)
test-$(1): ; $$(NIX_RUN)test/$(1)
endef

define vps_rule
.PHONY: $(1)
$(1): ; $$(NIX_RUN)deploy/$(1)

.PHONY: rollback-$(1)
rollback-$(1):
	$$(NIX_RUN)rollback/$(1)
endef

$(foreach machine,$(MACHINES),$(eval $(call machine_rule,$(machine))))
$(foreach vps,$(VPS),$(eval $(call vps_rule,$(vps))))

.PHONY: deps-my
deps-my:
	$(NIX_LOCK) $(foreach dep,$(DEPS_MY),--update-input $(dep))

.PHONY: deps-nixos
deps-nixos:
	$(NIX_LOCK) $(foreach dep,$(DEPS_NIXOS),--update-input $(dep))

.PHONY: deps-misc
deps-misc:
	$(NIX_LOCK) $(foreach dep,$(DEPS_MISC),--update-input $(dep))