How to organize and manage the nixos configuration?
How to organize and manage the nixos configuration?

NixOS

How to organize and manage the nixos configuration?

Manage and organize your nixos configuration.

The Linux
Published in
6 min readMar 25, 2024

--

The most important thing for every nixos user is managing and organizing your nixos configuration into different subfolders or modules.

Otherwise, when you install other packages, services, programs, flakes, etc., in your one configuration file, it gets messy and eventually becomes unmaintainable.

This article will teach you how to manage and organize your nixos configuration like a pro. You can easily share with others on GitLab, GitHub, etc., such as your teammates and nixos community members.

By default, the nixos configuration is inside your /etc/nixos folder containing the following file.

├── configuration.nix
└── hardware-configuration.nix

0 directories, 2 files
  • The configuration.nixfile contains the system configuration.
  • The hardware-configuration.nix file contains hardware configurations.

Convert the configuration file into a submodule

The /etc/nixos folder is a default location for nixos configurations. You can convert your nixos configuration into a submodule with Nix inbuilt module functionality.

For example, To convert nixos configurations into submodules, I create a separate programs.nix file inside the /etc/nixos/programs folder. Inside the programs.nix file, I configured the zsh program, and you can add any other program as well.

# programs/programs.nix

{config, pkgs, inputs, ... }:
{
programs = {
zsh = {
enable = true;
autosuggestions.enable = true;
zsh-autoenv.enable = true;
syntaxHighlighting.enable = true;
ohMyZsh = {
enable = true;
theme = "robbyrussell";
plugins = [
"git"
"npm"
"history"
"node"
"rust"
"deno"
];
};
};
};
}

After /etc/nixos folder structure looks like this.

├── configuration.nix
├── programs/programs.nix
└── hardware-configuration.nix

0 directories, 2 files

Last, import the programs.nix file inside the configuration.nixfile.

# configuration.nix
{ config, pkgs, ... }:
{

imports =
[
./hardware-configuration.nix

./programs/programs.nix # import here

./services/services.nix
];

...
system.stateVersion = "23.05";
}

Now rebuild your nixos system to see the change. You can now convert other things, like services, users, etc., and convert into a submodule. It helps you to manage nixos configuration more easily.

Move the nixos configuration into a different folder

But you can not change the default /etc/nixos location. This is the main problem when you edit the configuration.nix file or any other file inside the /etc/nixos folder.

It asks for root user permission whenever you edit or delete a file, which takes too much time. It even requires sudo permission when you push your code into GitLab, GitHub, etc.

The best solution is to move configurations to a different folder or location so we can easily edit the file.

Usually, you can not change the configuration folder, but we can with Nix Flake. Without Flake, you can not change the default location of your nixos configuration.

To move the nixos configuration to a different folder, first configure your nixos with Flake, and even you can use Home manage with (config it's using Flake, and it is not compulsory) Flake, then follow these steps.

  1. Take a Backup
  2. Move the nixos configuration
  3. Change Permission
  4. Build your package

Take a Backup

To start doing anything with nixos configuration. An important step is to take a backup of your nixos configuration folder. In the future, if some things happen, restore your backup.

For backup, run the following command.

sudo cp -r  /etc/nixos /etc/nixos.backup

To verify whether your backup is created or not, run the following command.

➜  /etc ls -l  | grep 'nixos'     

drwxr-xr-x 5 root root 4096 Mar 21 21:52 nixos
drwxr-xr-x 5 root root 4096 Mar 23 14:55 nixos.backup

Move the nixos configuration.

After creating a backup, the next step is to move your nixos configuration to a different location.

In my case, I moved my configuration to the ~/nixos/config folder. Feel free to choose any location that suits your requirements.

To move your nixos configuration, you can select the cp (copy) or mv command. I prefer the mv command.

sudo cp -r  /etc/nixos  ~/nixos-config/
# or
sudo mv /etc/nixos ~/nixos-config/

Change Permission

When we move the nixos configuration to a different location, you can not edit the nixos-config folder file because of the root user permissions.

You need sudo to edit the nixos-config folder file. You can check your file user permissions with the following command.

ls -la nixos-config                     
total 40
-rw-r--r-- 1 root root 6817 Mar 22 13:43 configuration.nix
-rw-r--r-- 1 root root 7364 Mar 21 21:52 flake.lock
-rw-r--r-- 1 root root 955 Mar 22 23:14 flake.nix
-rw-r--r-- 1 root root 1511 Oct 1 15:52 hardware-configuration.nix
-rw-r--r-- 1 root root 3706 Mar 21 22:29 home.nix
drwxr-xr-x 3 root root 4096 Mar 10 14:35 nixvim
-rw-r--r-- 1 root root 49 Mar 10 15:14 nixvim.nix

To solve this issue, you need to change the file user permissions. You can use the chown command to do that.

sudo chown <your-user-name> -R ~/nixos-config

# Working Example with officialrajdeepsingh user.
sudo chown officialrajdeepsingh -R ~/nixos-config

To check your current login user, use the following command.

➜ whoami
officialrajdeepsingh

➜ echo $USER;
officialrajdeepsingh

You can recheck whether your file user permissions are changed or not with the following command.

➜  ➜  ~ ls -la ~/nixos-config                                  
total 40
-rw-r--r-- 1 officialrajdeepsingh root 6817 Mar 22 13:43 configuration.nix
-rw-r--r-- 1 officialrajdeepsingh root 7364 Mar 21 21:52 flake.lock
-rw-r--r-- 1 officialrajdeepsingh root 955 Mar 22 23:14 flake.nix
-rw-r--r-- 1 officialrajdeepsingh root 1511 Oct 1 15:52 hardware-configuration.nix
-rw-r--r-- 1 officialrajdeepsingh root 3706 Mar 21 22:29 home.nix
drwxr-xr-x 3 officialrajdeepsingh root 4096 Mar 10 14:35 nixvim
-rw-r--r-- 1 officialrajdeepsingh root 49 Mar 10 15:14 nixvim.nix
-rw-r--r-- 1 officialrajdeepsingh root 991 Mar 21 22:27 text.nix

Build your package

Previous we build nixos with the following command

sudo nixos-rebuild switch --flake /etc/nixos#default

For now, we build nixos using the following command.

sudo nixos-rebuild switch --flake <your-config-path>#default

# Working example
sudo nixos-rebuild switch --flake ~/nixos-config/#default

Common Errors

  1. Related to Git
  2. Related to built

Related to Git

If you move your configuration, you may face minor issues like this.

➜  nixos-config lazygit
2024/03/23 15:30:31 An error occurred! Please create an issue at: https://github.com/jesseduffield/lazygit/issues

*errors.errorString fatal: detected dubious ownership in repository at '/home/officialrajdeepsingh/nixos-config'
To add an exception for this directory, call:

git config --global --add safe.directory /home/officialrajdeepsingh/nixos-config

github.com/jesseduffield/lazygit/pkg/commands/oscommands/cmd_obj_runner.go:200 (0x93f4d4)
github.com/jesseduffield/lazygit/pkg/commands/oscommands/cmd_obj_runner.go:107 (0x93e55c)
github.com/jesseduffield/lazygit/pkg/commands/oscommands/cmd_obj_runner.go:71 (0x93df2a)
github.com/jesseduffield/lazygit/pkg/commands/oscommands/cmd_obj.go:194 (0x93ce89)
github.com/jesseduffield/lazygit/pkg/commands/git_commands/status.go:62 (0x96ea4e)
github.com/jesseduffield/lazygit/pkg/commands/git_commands/status.go:56 (0xabf10e)
github.com/jesseduffield/lazygit/pkg/gui/recent_repos_panel.go:11 (0xabf0f8)
github.com/jesseduffield/lazygit/pkg/gui/gui.go:803 (0xab4938)
github.com/jesseduffield/lazygit/pkg/gui/layout.go:196 (0xabb426)
github.com/jesseduffield/lazygit/pkg/gui/layout.go:124 (0xabafa5)
github.com/jesseduffield/gocui@v0.3.1-0.20230806095321-ac7b03108825/gui.go:697 (0x74c039)
github.com/jesseduffield/gocui@v0.3.1-0.20230806095321-ac7b03108825/gui.go:1139 (0x74dd17)
github.com/jesseduffield/gocui@v0.3.1-0.20230806095321-ac7b03108825/gui.go:766 (0x74c69b)
github.com/jesseduffield/gocui@v0.3.1-0.20230806095321-ac7b03108825/gui.go:739 (0x74c35d)
github.com/jesseduffield/lazygit/pkg/gui/gui.go:669 (0xab343a)
github.com/jesseduffield/lazygit/pkg/gui/gui.go:675 (0xab3a45)
github.com/jesseduffield/lazygit/pkg/utils/utils.go:108 (0x80ef3c)
github.com/jesseduffield/lazygit/pkg/gui/gui.go:674 (0xab398e)
github.com/jesseduffield/lazygit/pkg/app/app.go:263 (0xadf5fd)
github.com/jesseduffield/lazygit/pkg/app/app.go:48 (0xadf592)
github.com/jesseduffield/lazygit/pkg/app/entry_point.go:150 (0xae176a)
github.com/jesseduffield/lazygit/main.go:23 (0xae2eb8)
runtime/internal/atomic/types.go:194 (0x43d81b)
runtime/asm_amd64.s:1650 (0x46d8c1)

It also gives you a solution command to fix this issue. For me, the solution command looks like this.

# Check your command output

➜ nixos-config git config --global --add safe.directory /home/officialrajdeepsingh/nixos-config

Related to built

This error occurs because git is enabled in your configuration folder. To solve the issue, you should track those files using git and then rebuild your system.

➜  nixos-config git:(main) ✗ sudo nixos-rebuild switch --flake ~/nixos-config/#default                                          
warning: Git tree '/home/officialrajdeepsingh/nixos-config' is dirty
error:
… while calling the 'seq' builtin

at /nix/store/zaza7mgggz4m5h6z18kajabf4wciaj47-source/lib/modules.nix:322:18:

321| options = checked options;
322| config = checked (removeAttrs config [ "_module" ]);
| ^
323| _module = checked (config._module);

… while evaluating a branch condition

at /nix/store/zaza7mgggz4m5h6z18kajabf4wciaj47-source/lib/modules.nix:261:9:

260| checkUnmatched =
261| if config._module.check && config._module.freeformType == null && merged.unmatchedDefns != [] then
| ^
262| let

(stack trace truncated; use '--show-trace' to show the full trace)

error: getting status of '/nix/store/0bvfdjlg9k2jrp2nvw347bxcaqc9x10r-source/programs': No such file or directory

Conclusion

The best solution is to divide your nixos configuration into different submodules or sub-folders. Use Home Manager and Flake to manage your nixos configuration.

Nix Flake allows you to move your configuration to a different location. I don't know if the home manager also gives functionality to change the default locations of the nixos configuration.

To learn more about NixOS and Linux stuff, follow the Linux publication on Medium and other updates. Follow me on Twitter (X) and Medium.

--

--

Rajdeep Singh
The Linux

JavaScript | TypeScript | Reactjs | Nextjs | Rust | Biotechnology | Bioinformatic | Frontend Developer | Author | https://linktr.ee/officialrajdeepsingh