Chapter 2 - Housekeeping
So far we’ve been working in one file with no VCS setup and no consistent formatting.
In this chapter we will…
- Get a Git repo set up for our system
- Learn how to compartmentalize our config into different files
- Do some setup for later chapters in
flake.nix
- Setup auto-formatting for our code
Setting Up Git
Before we do anything, we’ll need to make a .gitignore
.
- Create a
.gitignore
file to prevent unwanted files from being checked-in to VCS.- flake.nix
- .gitignore
- …
- I’d recommend this as a
.gitignore
. - Now initialize a new Git repo for your project.
We now have a git repo for our config!
Fragmenting Our Config
Now we’ll split up our NixOS config from one file, this is for organization.
-
Create files for each part of our config. To keep things clean, you probably want to put these in a folder called
modules
. flake.nix should not go in this folder, flake files must be in the root of the repo.Anything highlighted is something this guide is going to use later, so they’ll be required. Anything not highlighted is a suggestion based on how others tend to organize files, but pick what works best for you!
- flake.nix
- flake.lock
Directorymodules/
- config.nix Sets up basics and imports all other modules
- nix.nix Configures Nix itself within the VM
- graphics.nix (DEs, apps, theming, etc)
- shell.nix (Aliases, themes, etc)
- … Go wild!
-
You’ll also need to edit the path specified in
flake.nix
within themodules
array to point to./modules/config.nix
instead.
Importing other modules from config.nix
Now we need to tell NixOS what other files to load modules from.
To do this, in config.nix
create a new array called imports
(usually this is the very first config option).
This array should have the paths (relative to config.nix
) of other modules to load.
File Contents
Now fill in the modules we’ve created, use config.nix as reference, remember each module must return an attr set and are given a bunch of arguments. Here’s a good template for modules.
From there feel free to organize your source tree however you want!
Making nix.nix
I’ll leave exactly where to place certain options up to you, however this guide is going to require you to place the nix = { ... };
option inside modules/nix.nix
, we’ll be importing it from somewhere else later.
- Remove the
nix
attr set frommodules/config.nix
. - Then add the attr set in
modules/nix.nix
.
Passing inputs to our configuration
So when I said there were only two methods to install software I kinda lied…
Nix flakes are a wonderful way to get software straight from the repo the software is hosted on. We can see in our flake.nix
that we’re grabbing nixpkgs
from GitHub.
Since NixOS uses nixpkgs already, it’s already passed to us as an argument to modules. However, there may come a time that we need to access other inputs within our modules.
In order to do this we have to tell NixOS additional arguments we want passed.
- On the line with
outputs
, addinputs @
before the{
. - Create a new attr set in our call to
pkgs.lib.nixosSystem
calledspecialArgs
, we’ll want toinherit
ourinputs
here. - Now when we need to, we can specify
inputs
in our arguments on any module and have access to our flake inputs.
This may not help much as our only inputs at the moment are self
and nixpkgs
, but as we add more this will come in handy.
Auto-formatting
This step is optional but I’d recommend it if you’re like me and enjoy consistent style in your code.
Another type of output a nix flake can provide is a formatter
. This should be set to a package that has the ability to format Nix code, I’ll use alejandra (my personal preference) in this example.
To setup a formatter simply add it as an output to our flake.
Now we can format our code with Alejandra.