nixos-and-flakes-book/docs/best-practices/simplify-nixos-related-commands.md

65 lines
2.5 KiB
Markdown
Raw Normal View History

2023-07-04 04:18:46 +00:00
# Simplifying NixOS-Related Commands
2023-06-23 14:22:13 +00:00
2023-07-06 03:04:52 +00:00
To simplify NixOS-related commands, I utilize a Makefile, which proves to be very convenient.
2023-06-23 14:22:13 +00:00
2023-07-06 03:04:52 +00:00
Alternatively, you can also use similar tools like [just](https://github.com/casey/just) and [cargo-make](https://github.com/sagiegurari/cargo-make) for this purpose. Here, I will provide my approach as a reference.
2023-06-23 14:22:13 +00:00
2023-07-06 03:04:52 +00:00
Below is an example of how my Makefile looks:
2023-06-23 14:22:13 +00:00
2023-07-06 03:04:52 +00:00
>**NOTE**: The target names in the Makefile should not conflict with any file or directory names in the current directory. Otherwise, the targets will not execute.
2023-06-23 14:22:13 +00:00
2023-07-06 03:04:52 +00:00
```makefile
2023-06-23 14:22:13 +00:00
############################################################################
#
# Nix commands related to the local machine
#
############################################################################
deploy:
nixos-rebuild switch --flake . --use-remote-sudo
debug:
nixos-rebuild switch --flake . --use-remote-sudo --show-trace --verbose
update:
nix flake update
history:
nix profile history --profile /nix/var/nix/profiles/system
gc:
# remove all generations older than 7 days
sudo nix profile wipe-history --profile /nix/var/nix/profiles/system --older-than 7d
# garbage collect all unused nix store entries
sudo nix store gc --debug
############################################################################
#
2023-07-04 04:18:46 +00:00
# Idols: Commands related to my remote distributed building cluster
2023-06-23 14:22:13 +00:00
#
############################################################################
add-idols-ssh-key:
ssh-add ~/.ssh/ai-idols
aqua: add-idols-ssh-key
nixos-rebuild --flake .#aquamarine --target-host aquamarine --build-host aquamarine switch --use-remote-sudo
aqua-debug: add-idols-ssh-key
nixos-rebuild --flake .#aquamarine --target-host aquamarine --build-host aquamarine switch --use-remote-sudo --show-trace --verbose
ruby: add-idols-ssh-key
nixos-rebuild --flake .#ruby --target-host ruby --build-host ruby switch --use-remote-sudo
ruby-debug: add-idols-ssh-key
nixos-rebuild --flake .#ruby --target-host ruby --build-host ruby switch --use-remote-sudo --show-trace --verbose
2023-07-06 03:04:52 +00:00
idols: aqua ruby
2023-06-23 14:22:13 +00:00
2023-07-06 03:04:52 +00:00
idols-debug: aqua-debug ruby-debug
2023-06-23 14:22:13 +00:00
```
2023-07-06 03:04:52 +00:00
By Save the above Makefile to the root directory of your Nix flake. Then, I can use `make deploy` to deploy the configuration to my local machine, and `make idols` to deploy the configuration to all my remote servers.
2023-07-04 04:18:46 +00:00
This approach simplifies the execution of NixOS commands by abstracting them behind target names in the Makefile, providing a more user-friendly and convenient experience.