Go to file
2023-06-22 18:07:44 +08:00
.github/workflows ci: update GitHub actions 2023-04-24 11:32:04 +08:00
hosts hosts/plover: add TLS support for PostgreSQL 2023-06-22 18:05:54 +08:00
lib chore: reformat codebase 2023-06-22 11:12:43 +08:00
modules workflows/a-happy-gnome: add gnome-decoder to application list 2023-06-11 12:28:07 +08:00
pkgs gnome-shell-extension-fly-pie: 20 -> 21 2023-06-16 13:14:02 +08:00
secrets secrets/backup-archive: update remote backup Borg patterns 2023-06-11 22:36:20 +08:00
shells shells/cloud: add Terraform 2023-06-22 11:10:37 +08:00
templates chore: reformat codebase 2023-06-08 21:19:17 +08:00
users users/foo-dogsquared: use own Atuin sync server 2023-06-16 13:16:12 +08:00
.editorconfig hosts/plover: initialize Terraform configuration 2023-06-22 11:06:43 +08:00
.envrc Clean up files 2022-02-11 12:31:44 +08:00
.gitattributes git: remove linguist attributes 2023-06-22 18:07:44 +08:00
.gitignore hosts/plover: initialize Terraform configuration 2023-06-22 11:06:43 +08:00
.sops.yaml config: remove GCP key from cluster 2023-06-22 11:13:45 +08:00
default.nix chore: reformat codebase 2022-11-19 11:32:29 +08:00
flake.lock config: make configurable nixpkgs channel for images 2023-06-22 11:01:41 +08:00
flake.nix config: make configurable nixpkgs channel for images 2023-06-22 11:01:41 +08:00
images.toml config: make configurable nixpkgs channel for images 2023-06-22 11:01:41 +08:00
LICENSE LICENSE: update year 2023-01-05 11:08:22 +08:00
Makefile Makefile: separate update processes into using my own forks 2022-10-26 17:24:37 +08:00
README.adoc docs: update project setup notes 2023-06-22 18:04:02 +08:00
shell.nix shell.nix: reformat the application list 2023-06-22 11:11:37 +08:00
treefmt.toml hosts/plover: initialize Terraform configuration 2023-06-22 11:06:43 +08:00

This is my NixOS config as a Nix flake.

I finally have some time trying to grok flakes and redo my NixOS config from scratch after leaving it for some time (because I have work and have to quickly set things up without me trying to debug how NixOS works). Here is the result.

Getting started

Caution

Despite being a NixOS system, it isnt entirely reproducible to the point of installing this config will work out-of-the-box. It has some things needed in the first place to work like my external backup setup where it needs my SSH private keys to work in the first place. Nonetheless, the parts is it isnt reproducible is only about 10% of this config. Everything else is fine and dandy.

Oh right… Dont expect everything is working in each commit but Im doing my best to make them build successfully in each, yeah?

Also, this configuration is expected to use under non-restricted evaluation mode.

Hey there, fellow traveler! Youve fell into the rabbit hole we call Nix where it leads to other rabbit holes such as functional package management, reproducibility, declarative systems, and immutable operating systems.

This is one of the many tickets to that rabbit hole, allow me to be your guide in this mark:[painful] wonderful declarative journey. Hope to see you on the other side!

Installation

Since this uses Nix flakes, you should have Nix v2.8 and above installed.

If youre going to install one of my NixOS configs, be sure to download the unstable versions from their release page. As an additional option, you can also use my personalized NixOS installers which is primarily intended for me configs.

This primarily uses Nix flakes so you can have a preview of whats available in my config.

nix flake show github:foo-dogsquared/nixos-config

It should export my NixOS configurations of my different hosts (of only one so far excluding VMs and VPSs ;p) among other things. To install it, run the nixos-install --flake github:foo-dogsquared/nixos-config#ni. (Please see the respective appropriate host README for more information.)

Channels support

While this primarily uses flakes as its main form of distribution, this project does keep some use cases for traditional channels. Its not guaranteed to be good as using it With flakes but its an option.

The entry point is found at ./default.nix. However, you have to keep some limitations and guidelines in mind.

  • It exports an attribute based from the NUR template.

    Several exports includes…

    • My custom library at lib.

    • Custom NixOS modules at modules.

    • Custom home-manager modules at hmModules.

    • An overlay of my custom packages at overlays.foo-dogsquared-pkgs.

    • My packages as the rest of the top-level attributes from the attrset.

  • Keep in mind it doesnt export the NixOS hosts and home-manager user configurations. It would be pointless as it is duplicating effort plus I really like managing my NixOS config more with the flakes way compared to setting up channels. It has a lot of advantages such as the ease of provisioning and updating your setups along with its dependencies, enforcing certain values in a certain attribute that can be seen in the revised Nix CLI, and nicer interface overall. While possible with channels, this is just better experience overall and I have no interest in maintaining setups in both ways.

Whats in my flake?

You can see the full details with nix flake show. As a helpful summary, heres what my flake should contain sorting from the most interesting and helpful outputs to the most boring and unnecessary.

  • packages contains my custom packages and some of usual images of several hosts which is nice for easily fetching custom images.

  • overlays contains the overlay for extending nixpkgs with my packages. If you want to use my packages alongside the nixpkgs attribute then this is what youre looking for.

  • homeModules are where my custom home-manager modules to be exported. [1]

  • nixosModules are composed from NixOS modules defined in ./modules/nixos. It can be used as additional modules for your own NixOS config in case you want to use mine for whatever reason. There are some niceties in there.

    A few examples include:

    • A NixOS module for Pop launcher plugins and scripts.

    • Several service modules for archiving with yt-dlp, gallery-dl, and ArchiveBox.

    • My themes which contain full desktop environments which is nice for quickly initializing a NixOS configuration.

  • devShells from my custom environments. Similar to homeManagerConfigurations, you can easily use it outside of NixOS.

  • homeConfigurations contains my various home-manager configurations from ./users/home-manager/. The neat thing about it is you can easily install it in a non-NixOS Linux distro.

  • deploy are nodes to be deployed by deploy-rs. Its nice and currently not all of the nodes are publicly committed which is less useful but itll be someday.

  • nixosConfigurations which is where you can install my various NixOS configurations directly (e.g., nixos-install --flake github:foo-dogsquared/nixos-config#HOST). This mainly uses the hosts configuration.

  • templates which contains my templates. Though, these are just templates mostly for my own purposes so it is not as useful as the other outputs.

  • hydraJobs contains Hydra build jobs where it is primarily used for my test Hydra instance.

  • lib is defined from my custom library. It mainly assumes it is to be included in nixpkgs standard library (i.e., import ./lib { lib = inputs.nixpkgs.lib; }). Its another unnecessary export but its there.

  • formatter is the preferred formatter to be used for my Nix files. Nothing special here.

  • checks contains checks for several outputs and are mainly for internal purposes. Theres no use for anyone else, really. :(

What should not be here?

Despite being a NixOS configuration, this is not meant to be fully reproducible by anyone. There are still some things that would need to be privately held as indicated by the following list.

  • Associated private keys: GPG, SSH, age, you name it. They are used with a secret management tool (see Secrets management for more information) to encrypt the more sensitive parts of the system such as credentials and environment files.

  • Disposable hosts configurations. They will typically just make a messier mess than the current situation.

Though the unreproducible part is only like 10% of the whole configuration, it can be successfully deployed by anyone. Keep in mind, it comes with a few restrictions due to the lack of the appropriate credentials.

  • Certain tasks will not start. Most of the project tasks found in this repo requires the associated private key with the task. An example would be the Borg backup task where it needs several files and credentials locked from the secrets management tool.

  • Certain components will be missing. Most notably, the associated SSH key for the hosts. You wont be able to connect to the host if you dont have the private key.

  • Not to mention not all modules listed committed here are up-to-date. Though this only applies to non-critical services like the multimedia archiving service.

The remote repo

For a complete overkill, we use a CI to further the configuration abomination. It uses GitHub workflows to enable things such as…

  • Automatically building my custom packages and sending them to my binary cache to easily distribute it for others.

  • Building my personalized NixOS installers and making a release out of it.

  • Testing the flake outputs (in progress lel).

Project structure

Last and foremost, we have the project directory — the environment youll be living in for the next year, tinkering your Nix configs. . It is required to be familiar with the workspace, after all. My configuration takes a lot of cues from devos (which is unfortunately is a lot to take if youre just beginning to grok Nix as a whole).

My NixOS config should look like the following:

nixos-config
├── hosts/
├── lib/
├── modules/
├── pkgs/
├── scripts/
├── secrets/
├── shells/
├── templates/
├── users/
├── default.nix
├── flake.lock
├── flake.nix
├── images.toml
└── README.adoc

One of the more notable files here when first start looking is the ./images.toml file where it contains a description of the images. The data is then used for certain functions in the flake definition file (i.e., flake.nix). The data can contain from what image output format it is intended to be deployed (most likely used anyways), the nixpkgs channel to be used, and its system (e.g., x86_64-linux).

Most of the said folders are related to a flake output attribute, see Whats in my flake? for more details.

  • ./hosts/ contain machine-specific configuration. This usually configures like the hardware setup, timezone, and users. Host configurations are also exported in the flakes in outputs.nixosConfigurations.

  • ./modules/ contain my custom modules including NixOS and home-manager modules. For more information, see the related documentation.

  • ./pkgs/ contains my custom packages. It is exported in the flakes at outputs.packages compiled through various systems.

  • ./scripts/ contains various scripts for various purposes. Should be self-explanatory.

  • ./secrets/ contains my cluster-wide secrets managed with sops and sops-nix. Take note, each component (e.g., hosts, modules, users) could have their own specific secrets.

  • ./shells/ contains my development shells for interacting with the usual type of projects. Setting this up can bring benefits outside of NixOS (unless youre interacting with projects with any OpenGL-related stuff). [2]

  • ./templates/ contains my custom templates handy for quickly initializing for various types of projects.

  • ./users/ contains my home-manager configurations. It is exported in the flakes at outputs.homeConfigurations. For more information, see the related documentation.

Secrets management

This repo uses sops as the main secret management tool. For integrating this into NixOS, I use sops-nix.

To get started using sops, I recommend to take a look at .sops.yaml file which secrets belong to whom. Then edit a secrets with sops PATH_TO_SECRET. Take note, you need to respective keys to edit the secret in the first place. If you edit ./secrets/backup-archive.yaml for example, it needs one of the keys (either my age and GPG key or the age key from host ni).

Using parts of my configuration

Hey there, stranger. Wanted to try parts of my configuration but dont want to copy it outright since youre feeling lazy or what-have-you? I made my configuration to be easy to use and integrate into your system.

Heres how…

With flakes

This is the recommended method since I primarily use flakes for this project. Not to mention that with flakes, this is easier than ever to make use parts of my configuration.

To start, you can simply add my flake to your list of flake inputs.

inputs.foo-dogsquared-nixos-config.url = "github:foo-dogsquared/nixos-config";

Then, you could use parts of the config as exported from my flake which you can refer back to Whats in my flake?.

For example, you could make use of my packages by adding them as an overlay which is recommended if youre going to use my NixOS modules anyways. Heres one way to put as part of your NixOS configuration…

{
  nixpkgs.overlays = [
    inputs.foo-dogsquared-nixos-config.overlays.default
  ];
}

or import them as part of nixpkgs.

import nixpkgs {
  system = "x86_64-linux";
  overlays = [
    inputs.foo-dogsquared-nixos-config.overlays.default
  ];
}

If youre going to use my stuff, why dont take a gander and try my non-personal parts of the configuration such as my NixOS modules and home-manager modules? In that case, you can simply plop them into your list of imports for your NixOS configuration like so.

{
  imports = [
    inputs.foo-dogsquared-nixos-config.nixosModules.programs
    inputs.foo-dogsquared-nixos-config.nixosModules.services
    inputs.foo-dogsquared-nixos-config.nixosModules.workflows
  ];

  # Use my GNOME desktop configuration for some reason.
  workflows.workflows.a-happy-gnome.enable = true;
}

With channels

The traditional way of managing stuff with channels. Though, I have made some efforts to make it easy to use without flakes, I cannot guarantee its good compared to using it with flakes.

Warning
You cannot install my NixOS configurations at all with channels so theres another reason why (whether is valid or not is completely up to you).

To start, as root, you have to add my project into the channels list…

nix-channel --add "https://github.com/foo-dogsquared/nixos-config/archive/master.tar.gz" foo-dogsquared-nixos-config
nix-channel --update

then import my config as part of your configuration.

import <foo-dogsquared-nixos-config> { inherit pkgs; }

You can see ./default.nix to see more details but there are general guidelines to the attributes that is contained in this file which is outlined in Channels support section.

Heres an example snippet in a NixOS config making use of my configuration without flakes:

let
  foo-dogsquared-nixos-config = import <foo-dogsquared-nixos-config> { inherit pkgs; };
in {
  imports = [
    foo-dogsquared-nixos-config.modules.programs
    foo-dogsquared-nixos-config.modules.services
    foo-dogsquared-nixos-config.modules.workflows
  ];

  # Still using my GNOME desktop configuration for some reason.
  workflows.workflows.a-happy-gnome.enable = true;
}

With manual fetching

If you really dont want to manage stuff with channels or with flakes for some reason, I suppose you can just use something like niv. You could also pin my config similarly to how you can pin nixpkgs then use it as if you manage it as described from With channels.

Heres a snippet of using it as part of a NixOS configuration.

let
  foo-dogsquared-nixos-config = import (fetchTarball "https://github.com/foo-dogsquared/nixos-config/archive/35c27749c55077727529f412dade862e4deb2ae8.tar.gz") { inherit pkgs; };
in {
  imports = [
    foo-dogsquared-nixos-config.modules.programs
    foo-dogsquared-nixos-config.modules.services
    foo-dogsquared-nixos-config.modules.workflows
  ];

  # Still using my GNOME desktop configuration for some reason.
  workflows.workflows.a-happy-gnome.enable = true;
}

TODO

In order of priority:

  • ✓ Create custom modules.

  • ✓ Create a themes system similar to this NixOS config.

  • ✓ Create a good workflow for creating ISOs.

  • ✓ Create development shells.

  • ✓ Manage secrets with agenix.

  • ❏ Create a good workflow for tests.

  • ✓ Automate backups with NixOS config.

  • ✓ Create custom packages and export it to flakes. (Maybe consider making it to upstream)

  • ✓ Create cluser-wide configs.

  • ✓ Create host-wide configs.

  • ✓ Create user-specific configs with home-manager.

  • Steal Get some ideas from this overengineered template.

  • ✓ Make use of other established utilities such as digga, flake-utils-plus, and home-manager once Im familiar to create my own Nix programs.

Out of scope:

  • Set most program configurations with Nix. This is especially applicable to programs I always use such as my text editor configs.

    • The reason: managing them can be chaotic when applied with Nix. The potential for it is pretty nice especially when seen with similar implementations such as Guix home environment. However, I dont want to rebuild it every time I change it.

    • Plus, most of my applications are now installed using Flatpak anyways. It is a tad easier to manage configurations installed this way (e.g., just copy ~/.var between your systems or make backups with it).

  • Migration of my dotfiles. I still use it on other non-NixOS systems.

  • To be a configuration framework. This is my personal configuration which means experimentation will always be here. Sudden changes can happen at any point.

Frequently asked questions (FAQ)

More like "Most anticipated questions (MAQ)" since almost no one asked me about this yet. :) However, it doesnt roll off the tongue.

  1. Im new to Nix and NixOS, where should I start?

    Oh no, youve seen the multiple configurations from other systems, didnt you?

    I hope youre ready for some time understanding because the learning curve is steeper than the Troll Wall. Ive written a blog post regarding my experience with Nix with a bunch of links for getting started. Fortunately [3], it is mostly the same experience now that Ive revisited it. (That said, Nix Pills is getting better!)

    For a more practical start, I recommend to look into several configurations to see the general gist of configuration with Nix. Heres a few I personally dug into:

  2. Should I use NixOS?

    I feel like this answer is enough but Ill give my take on it regardless.

    If you are looking forward to debugging your configuration which consists of finding multiple sources from the manuals from the website, reading the source code from the appropriate point in nixpkgs, and repeatedly testing your NixOS configurations with each change then this is for you.

    Entering the Nix ecosystem at the beginning requires high level of engagement to learn the ropes even if your only intention is to create a basic reproducible operating system ala-Ansible.

    • Youll encounter vague and unique errors as the likes of NixOS is a unique world (the infamous infinite recursion, anyone?).

    • Youll appreciate technical writers as the documentation department is lacking.

    • Youll read outdated unofficial guides, resources, and the source code as you dive deeper into Nix.

    • Youll get used to the unfortunate UX that is the toolings around Nix (even the revised Nix CLI has its problems).

    Theres nothing better with NixOS than the (un)official NixOS experience™.

    Once you passed the initial curve and the difficulties that come with Nix, the benefits are immense.

    • Using Nix beyond package management.

      • As a universal build system due to its ability to install and integrate multiple versions of the same program without problems and decent support for different projects use cases (i.e., programming languages, build systems).

      • As a virtualenv-like environment useful for managing development environments.

      • As a way to manage clusters and infrastructure (i.e., NixOS for your installations, nixops for deployment, Hydra for continuous integration).

    • You can make use of Nixs large ecosystem of packages and tools from nixpkgs and beyond.

    • It is easier to distribute and verify your builds as Nix also has toolings available for those purposes. [4]

    If you intend to use NixOS, I recommend to start small and install Nix on a non-NixOS distro and use it as a way to create reproducible development environment, be familiar to its build process (I recommend reading Nix Pills to get started), get intimate with systemd, and then test NixOS in a virtual machine.

  3. Any requirements for this setup?

    You only need Nix 2.6 installed with the experimental features for Nix command and flakes enabled (i.e., experimental-features = nix-command flakes).

  4. Hey! I have an error something like /nix/${HASH}-nmd/ from my installation. Why it doesnt work?

    This error seems to be related to home-manager. See this issue for more details. Basically, you have to add --impure flag when running nixos-install.

  5. What is my experience on Nix flakes?

    I LIKE IT! There are still some hiccups like the strict tie-in to Git which leads to strange experience like having to add untracked files just to be recognized. However, this feature is still in the works so no surprise there.

    With that said, it is workable to be a daily part of the NixOS experience especially with the benefits of Nix flakes.

    • More user-friendly compared to Nix channels.

    • Just one reference file to know what is being exported from a project.

    • Unification with commonly-agreed schemas.

    This feature somewhat helped me understand more about the Nix language, oddly. I guess those lightbulb moments can come from random places.

  6. Is this based from devos?

    Yes but Ive created this from scratch. One of my goals when creating this config is to use as little dependency as much as possible and to understand more whats going under the hood with Nix. At the time Ive rewritten this configuration, I have little idea about Nix as a language and I find devos to be overwhelming so I doubled down to create from scratch.

    However, I can say that my configuration is moving in a similar direction to devos (which is intended to be modified anyways) as I explore more of Nix. In fact, Ive based much of my config from it and may even be ended up as one. It is nice that such a project exists serving as a reference for bigger configurations.

  7. Whats up with some error regarding something in restricted mode?

    If you found some error regarding something in restricted mode, you can run the installation with --impure flag (i.e., nixos-install --impure --flake github:foo-dogsquared/nixos-config#ni).

Inspirations and acknowledgment

I stole got several parts of this configuration from the following projects:

This project is licensed under MIT license. I just chose it to make it easier to upstream parts of this project to nixpkgs and to make it easier to copy it without much problems (just dont forget to add attribution as indicated from the license). Please see ./LICENSE for the full text.


1. This is more useful than my NixOS modules.
2. Since packages brought from Nix shells can only work with the store, a container might be better at some situations.
3. Or unfortunately from a different perspective.
4. Though, other packaging formats also has those but for Nix, reproducibility is one of the focus.