2023-07-04 04:18:46 +00:00
|
|
|
# Simplifying NixOS-Related Commands
|
2023-06-23 14:22:13 +00:00
|
|
|
|
2024-03-16 11:29:05 +00:00
|
|
|
To simplify NixOS-related commands, I utilize [just](https://github.com/casey/just), which
|
|
|
|
proves to be very convenient.
|
2023-06-23 14:22:13 +00:00
|
|
|
|
2024-03-16 11:29:05 +00: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 14:22:13 +00:00
|
|
|
|
2024-01-11 03:17:05 +00:00
|
|
|
Below is an example of how my Justfile looks:
|
2023-06-23 14:22:13 +00:00
|
|
|
|
2024-03-16 11:29:05 +00:00
|
|
|
> The latest Justfile I'm using:
|
|
|
|
> [ryan4yin/nix-config/Justfile](https://github.com/ryan4yin/nix-config/blob/main/Justfile)
|
2024-01-11 04:44:43 +00:00
|
|
|
|
|
|
|
```Makefile
|
2024-01-11 03:17:05 +00:00
|
|
|
# just is a command runner, Justfile is very similar to Makefile, but simpler.
|
2023-06-23 14:22:13 +00:00
|
|
|
|
|
|
|
############################################################################
|
|
|
|
#
|
|
|
|
# Nix commands related to the local machine
|
|
|
|
#
|
|
|
|
############################################################################
|
|
|
|
|
|
|
|
deploy:
|
2024-01-11 03:17:05 +00:00
|
|
|
nixos-rebuild switch --flake . --use-remote-sudo
|
2023-06-23 14:22:13 +00:00
|
|
|
|
|
|
|
debug:
|
2024-01-11 03:17:05 +00:00
|
|
|
nixos-rebuild switch --flake . --use-remote-sudo --show-trace --verbose
|
2023-06-23 14:22:13 +00:00
|
|
|
|
2024-01-11 05:30:33 +00:00
|
|
|
up:
|
2024-01-11 03:17:05 +00:00
|
|
|
nix flake update
|
2023-06-23 14:22:13 +00:00
|
|
|
|
2024-01-11 05:30:33 +00:00
|
|
|
# Update specific input
|
|
|
|
# usage: make upp i=home-manager
|
|
|
|
upp:
|
|
|
|
nix flake lock --update-input $(i)
|
|
|
|
|
2023-06-23 14:22:13 +00:00
|
|
|
history:
|
2024-01-11 03:17:05 +00:00
|
|
|
nix profile history --profile /nix/var/nix/profiles/system
|
2023-06-23 14:22:13 +00:00
|
|
|
|
2024-01-11 05:30:33 +00:00
|
|
|
repl:
|
|
|
|
nix repl -f flake:nixpkgs
|
|
|
|
|
|
|
|
clean:
|
2024-01-11 03:17:05 +00: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 14:22:13 +00:00
|
|
|
|
2024-01-11 05:30:33 +00:00
|
|
|
gc:
|
2024-01-11 03:17:05 +00:00
|
|
|
# garbage collect all unused nix store entries
|
2024-01-11 05:30:33 +00:00
|
|
|
sudo nix-collect-garbage --delete-old
|
2023-06-23 14:22:13 +00:00
|
|
|
|
|
|
|
############################################################################
|
|
|
|
#
|
2024-01-11 03:17:05 +00:00
|
|
|
# Idols, Commands related to my remote distributed building cluster
|
2023-06-23 14:22:13 +00:00
|
|
|
#
|
|
|
|
############################################################################
|
|
|
|
|
|
|
|
add-idols-ssh-key:
|
2024-01-11 03:17:05 +00:00
|
|
|
ssh-add ~/.ssh/ai-idols
|
2023-06-23 14:22:13 +00:00
|
|
|
|
|
|
|
aqua: add-idols-ssh-key
|
2024-01-11 03:17:05 +00:00
|
|
|
nixos-rebuild --flake .#aquamarine --target-host aquamarine --build-host aquamarine switch --use-remote-sudo
|
2023-06-23 14:22:13 +00:00
|
|
|
|
|
|
|
aqua-debug: add-idols-ssh-key
|
2024-01-11 03:17:05 +00:00
|
|
|
nixos-rebuild --flake .#aquamarine --target-host aquamarine --build-host aquamarine switch --use-remote-sudo --show-trace --verbose
|
2023-06-23 14:22:13 +00:00
|
|
|
|
|
|
|
ruby: add-idols-ssh-key
|
2024-01-11 03:17:05 +00:00
|
|
|
nixos-rebuild --flake .#ruby --target-host ruby --build-host ruby switch --use-remote-sudo
|
2023-06-23 14:22:13 +00:00
|
|
|
|
|
|
|
ruby-debug: add-idols-ssh-key
|
2024-01-11 03:17:05 +00:00
|
|
|
nixos-rebuild --flake .#ruby --target-host ruby --build-host ruby switch --use-remote-sudo --show-trace --verbose
|
2023-06-23 14:22:13 +00:00
|
|
|
|
2024-01-11 03:17:05 +00:00
|
|
|
kana: add-idols-ssh-key
|
|
|
|
nixos-rebuild --flake .#kana --target-host kana --build-host kana switch --use-remote-sudo
|
2023-06-23 14:22:13 +00:00
|
|
|
|
2024-01-11 03:17:05 +00: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 14:22:13 +00:00
|
|
|
```
|
|
|
|
|
2024-03-16 11:29:05 +00: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 03:17:05 +00:00
|
|
|
|
2024-03-16 11:29:05 +00: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.
|