nixos-and-flakes-book/docs/nixos-with-flakes/other-useful-tips.md

116 lines
4.3 KiB
Markdown
Raw Normal View History

2023-07-04 06:18:46 +02:00
# Other Useful Tips
2023-06-23 14:29:12 +02:00
2024-01-23 02:48:55 +01:00
## Show detailed error messages
You can always try to add `--show-trace -L` to the `nixos-rebuild` command to get the detailed error message if you encounter any errors during the deployment. e.g.
```bash
cd /etc/nixos
sudo nixos-rebuild switch --flake .#myhost --show-trace -L
```
2023-07-04 06:18:46 +02:00
## Managing the Configuration with Git
2023-06-23 14:29:12 +02:00
2023-07-04 06:18:46 +02:00
NixOS configuration, being a set of text files, is well-suited for version control with Git. This allows easy rollback to a previous version in case of issues.
2023-06-23 14:29:12 +02:00
2024-01-11 04:06:59 +01:00
> NOTE: When using Git, Nix ignores all files that are not tracked by Git. If you encounter an error in Nix stating that a particular file is not found, it may be because you haven't `git add`ed it.
2023-12-16 13:54:35 +01:00
By default, NixOS places the configuration in `/etc/nixos`, which requires root permissions for modification, making it inconvenient for daily use. Thankfully, Flakes can help solve this problem by allowing you to place your flake anywhere you prefer.
2023-06-23 14:29:12 +02:00
2023-07-04 06:18:46 +02:00
For example, you can place your flake in `~/nixos-config` and create a symbolic link in `/etc/nixos` as follows:
2023-06-23 14:29:12 +02:00
```shell
2023-07-04 06:18:46 +02:00
sudo mv /etc/nixos /etc/nixos.bak # Backup the original configuration
2023-06-23 14:29:12 +02:00
sudo ln -s ~/nixos-config/ /etc/nixos
2023-07-04 06:18:46 +02:00
# Deploy the flake.nix located at the default location (/etc/nixos)
2023-06-23 14:29:12 +02:00
sudo nixos-rebuild switch
```
2023-07-04 06:18:46 +02:00
This way, you can use Git to manage the configuration in `~/nixos-config`. The configuration can be modified with regular user-level permissions and does not require root ownership.
2023-06-23 14:29:12 +02:00
2023-07-04 06:18:46 +02:00
Another approach is to delete `/etc/nixos` directly and specify the configuration file path each time you deploy it:
2023-06-23 14:29:12 +02:00
```shell
sudo mv /etc/nixos /etc/nixos.bak
cd ~/nixos-config
2024-02-14 09:00:16 +01:00
# `--flake .#my-nixos` deploys the flake.nix located in
# the current directory, and the nixosConfiguration's name is `my-nixos`
sudo nixos-rebuild switch --flake .#my-nixos
2023-06-23 14:29:12 +02:00
```
2023-07-04 06:18:46 +02:00
Choose the method that suits you best. Afterward, system rollback becomes simple. Just switch to the previous commit and deploy it:
2023-06-23 14:29:12 +02:00
```shell
cd ~/nixos-config
2023-07-04 06:18:46 +02:00
# Switch to the previous commit
2023-06-23 14:29:12 +02:00
git checkout HEAD^1
2023-07-08 12:31:45 +02:00
# Deploy the flake.nix located in the current directory,
2024-02-14 09:00:16 +01:00
# with the nixosConfiguration's name `my-nixos`
sudo nixos-rebuild switch --flake .#my-nixos
2023-06-23 14:29:12 +02:00
```
2023-07-04 06:18:46 +02:00
More advanced Git operations are not covered here, but in general, rollback can be performed directly using Git. Only in cases of complete system crashes would you need to restart into the bootloader and boot the system from a previous historical version.
2023-06-23 14:29:12 +02:00
2023-07-04 06:18:46 +02:00
## Viewing and Deleting Historical Data
2023-06-23 14:29:12 +02:00
2023-07-04 06:18:46 +02:00
As mentioned earlier, each NixOS deployment creates a new version, and all versions are added to the system's boot options. In addition to restarting the computer, you can query all available historical versions using the following command:
2023-06-23 14:29:12 +02:00
```shell
nix profile history --profile /nix/var/nix/profiles/system
```
2023-07-04 06:18:46 +02:00
To clean up historical versions and free up storage space, use the following command:
2023-06-23 14:29:12 +02:00
```shell
2023-07-04 06:18:46 +02:00
# Delete all historical versions older than 7 days
2023-07-08 12:31:45 +02:00
sudo nix profile wipe-history --older-than 7d --profile /nix/var/nix/profiles/system
2023-06-23 14:29:12 +02:00
2023-07-04 06:18:46 +02:00
# Run garbage collection after wiping history
2023-06-23 14:29:12 +02:00
sudo nix store gc --debug
```
2024-03-05 07:36:14 +01:00
## Why some packages are installed?
To find out why a package is installed, you can use the following command:
2024-03-05 13:57:53 +01:00
1. Enter a shell with `nix-tree` available: `nix shell nixpkgs#nix-tree nixpkgs#ripgrep`
2024-03-05 07:36:14 +01:00
1. ` nix-store --gc --print-roots | rg -v '/proc/' | rg -Po '(?<= -> ).*' | xargs -o nix-tree`
1. `/<package-name>` to find the package you want to check.
1. `w` to show the package is depended by which packages, and the full dependency chain.
2023-06-23 17:53:25 +02:00
2023-07-04 06:18:46 +02:00
## Reducing Disk Usage
2023-06-23 17:53:25 +02:00
2023-07-04 06:18:46 +02:00
The following configuration can be added to your NixOS configuration to help reduce disk usage:
2023-06-23 17:53:25 +02:00
```nix
{ lib, pkgs, ... }:
{
2023-07-04 06:18:46 +02:00
# ...
2023-06-23 17:53:25 +02:00
2023-07-04 06:18:46 +02:00
# Limit the number of generations to keep
2023-06-23 17:54:50 +02:00
boot.loader.systemd-boot.configurationLimit = 10;
2023-06-23 17:53:25 +02:00
# boot.loader.grub.configurationLimit = 10;
2023-07-04 06:18:46 +02:00
# Perform garbage collection weekly to maintain low disk usage
2023-06-23 17:53:25 +02:00
nix.gc = {
2023-06-23 17:54:50 +02:00
automatic = true;
dates = "weekly";
options = "--delete-older-than 1w";
2024-01-23 02:48:55 +01:00
};
2023-07-04 06:18:46 +02:00
# Optimize storage
# You can also manually optimize the store via:
2023-06-23 17:53:25 +02:00
# nix-store --optimise
2023-07-04 06:18:46 +02:00
# Refer to the following link for more details:
2023-06-23 17:53:25 +02:00
# https://nixos.org/manual/nix/stable/command-ref/conf-file.html#conf-auto-optimise-store
nix.settings.auto-optimise-store = true;
}
```
2023-07-04 06:18:46 +02:00
2023-07-08 06:55:41 +02:00
By incorporating this configuration, you can better manage and optimize the disk usage of your NixOS system.