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

90 lines
2.9 KiB
Markdown
Raw Normal View History

2023-07-04 06:18:46 +02:00
# Simplifying NixOS-Related Commands
2023-06-23 16:22:13 +02:00
To simplify NixOS-related commands, I utilize [just](https://github.com/casey/just), which
proves to be very convenient.
2023-06-23 16:22:13 +02:00
Alternatively, you can also use similar tools like Makefile or
[cargo-make](https://github.com/sagiegurari/cargo-make) for this purpose. Here, I will
provide my approach as a reference.
2023-06-23 16:22:13 +02:00
2024-01-11 04:17:05 +01:00
Below is an example of how my Justfile looks:
2023-06-23 16:22:13 +02:00
> The latest Justfile I'm using:
> [ryan4yin/nix-config/Justfile](https://github.com/ryan4yin/nix-config/blob/main/Justfile)
2024-01-11 05:44:43 +01:00
```Makefile
2024-01-11 04:17:05 +01:00
# just is a command runner, Justfile is very similar to Makefile, but simpler.
2023-06-23 16:22:13 +02:00
############################################################################
#
# Nix commands related to the local machine
#
############################################################################
deploy:
2024-01-11 04:17:05 +01:00
nixos-rebuild switch --flake . --use-remote-sudo
2023-06-23 16:22:13 +02:00
debug:
2024-01-11 04:17:05 +01:00
nixos-rebuild switch --flake . --use-remote-sudo --show-trace --verbose
2023-06-23 16:22:13 +02:00
up:
2024-01-11 04:17:05 +01:00
nix flake update
2023-06-23 16:22:13 +02:00
# Update specific input
# usage: make upp i=home-manager
upp:
nix flake update $(i)
2023-06-23 16:22:13 +02:00
history:
2024-01-11 04:17:05 +01:00
nix profile history --profile /nix/var/nix/profiles/system
2023-06-23 16:22:13 +02:00
repl:
nix repl -f flake:nixpkgs
clean:
2024-01-11 04:17:05 +01:00
# remove all generations older than 7 days
sudo nix profile wipe-history --profile /nix/var/nix/profiles/system --older-than 7d
2023-06-23 16:22:13 +02:00
gc:
2024-01-11 04:17:05 +01:00
# garbage collect all unused nix store entries
sudo nix-collect-garbage --delete-old
2023-06-23 16:22:13 +02:00
############################################################################
#
2024-01-11 04:17:05 +01:00
# Idols, Commands related to my remote distributed building cluster
2023-06-23 16:22:13 +02:00
#
############################################################################
add-idols-ssh-key:
2024-01-11 04:17:05 +01:00
ssh-add ~/.ssh/ai-idols
2023-06-23 16:22:13 +02:00
aqua: add-idols-ssh-key
2024-01-11 04:17:05 +01:00
nixos-rebuild --flake .#aquamarine --target-host aquamarine --build-host aquamarine switch --use-remote-sudo
2023-06-23 16:22:13 +02:00
aqua-debug: add-idols-ssh-key
2024-01-11 04:17:05 +01:00
nixos-rebuild --flake .#aquamarine --target-host aquamarine --build-host aquamarine switch --use-remote-sudo --show-trace --verbose
2023-06-23 16:22:13 +02:00
ruby: add-idols-ssh-key
2024-01-11 04:17:05 +01:00
nixos-rebuild --flake .#ruby --target-host ruby --build-host ruby switch --use-remote-sudo
2023-06-23 16:22:13 +02:00
ruby-debug: add-idols-ssh-key
2024-01-11 04:17:05 +01:00
nixos-rebuild --flake .#ruby --target-host ruby --build-host ruby switch --use-remote-sudo --show-trace --verbose
2023-06-23 16:22:13 +02:00
2024-01-11 04:17:05 +01:00
kana: add-idols-ssh-key
nixos-rebuild --flake .#kana --target-host kana --build-host kana switch --use-remote-sudo
2023-06-23 16:22:13 +02:00
2024-01-11 04:17:05 +01:00
kana-debug: add-idols-ssh-key
nixos-rebuild --flake .#kana --target-host kana --build-host kana switch --use-remote-sudo --show-trace --verbose
idols: aqua ruby kana
idols-debug: aqua-debug ruby-debug kana-debug
2023-06-23 16:22:13 +02:00
```
By Save this `Justfile` to the root directory of your Nix flake. Then, I can use
`just deploy` to deploy the configuration to my local machine, and `just idols` to deploy
the configuration to all my remote servers.
2024-01-11 04:17:05 +01:00
This approach simplifies the execution of NixOS commands by abstracting them behind target
names in the Justfile, providing a more user-friendly and convenient experience.