2021-12-16 14:12:15 -08:00
|
|
|
# Nix Starter Config
|
|
|
|
|
2022-04-24 06:14:49 -07:00
|
|
|
This is a simple nix flake for getting started with NixOS + home-manager.
|
|
|
|
|
2022-04-24 16:43:29 -07:00
|
|
|
# What this provides
|
2021-12-16 14:12:15 -08:00
|
|
|
|
2022-04-24 16:35:26 -07:00
|
|
|
- NixOS configuration on `nixos/configuration.nix`, accessible via
|
2022-04-24 17:16:16 -07:00
|
|
|
`nixos-rebuild --flake .`
|
2022-04-24 16:35:26 -07:00
|
|
|
- Home-manager configuration on `home-manager/home.nix`, accessible via
|
2022-04-24 17:16:16 -07:00
|
|
|
`home-manager --flake .`
|
2022-04-24 16:35:26 -07:00
|
|
|
- Basic boilerplate for adding custom packages (under `pkgs`) and overlays
|
2022-04-24 17:16:16 -07:00
|
|
|
(under `overlays`). Accessible on your system, home config, as well as `nix
|
2022-04-24 16:35:26 -07:00
|
|
|
build .#package-name`
|
|
|
|
- Boilerplate for custom NixOS (`modules/nixos`) and home-manager
|
|
|
|
(`modules/home-manager`) modules
|
2021-12-16 14:12:15 -08:00
|
|
|
|
2022-04-24 16:43:29 -07:00
|
|
|
# Getting started
|
2021-12-16 14:12:15 -08:00
|
|
|
|
2022-04-24 16:35:26 -07:00
|
|
|
Assuming you have a basic NixOS booted up (either live or installed, anything
|
2022-04-24 17:07:40 -07:00
|
|
|
works). [Here's a link to the latest NixOS downloads, just for
|
|
|
|
you](https://nixos.org/download#download-nixos).
|
2021-12-16 14:12:15 -08:00
|
|
|
|
2022-04-24 18:58:35 -07:00
|
|
|
## I like your funny words, magic man
|
2022-04-24 18:57:48 -07:00
|
|
|
|
|
|
|
Not sure what this all means?
|
|
|
|
|
|
|
|
Take a look at [the learn hub on the NixOS
|
|
|
|
website](https://nixos.org/learn.html) (scroll down to guides, the manuals, and
|
|
|
|
the other awesome learning resources).
|
|
|
|
|
|
|
|
Learning the basics of what Nix (the package manager) is, how the Nix language
|
|
|
|
works, and a bit of NixOS basics should get you up and running. Don't worry if
|
|
|
|
it seems a little confusing at first. Get confortable with the basic concepts
|
|
|
|
and come back here to get your feet wet, it's the best way to learn!
|
|
|
|
|
2022-04-24 16:43:29 -07:00
|
|
|
## The repo
|
2022-04-24 16:35:26 -07:00
|
|
|
|
2021-12-16 14:12:15 -08:00
|
|
|
- [Install git](https://nixos.wiki/wiki/git)
|
2022-04-24 17:07:40 -07:00
|
|
|
- [Hit "Use this
|
|
|
|
template"](https://github.com/Misterio77/nix-starter-config/generate) on this
|
|
|
|
repo (or clone this down and push to any another git remote)
|
2022-04-24 16:35:26 -07:00
|
|
|
|
|
|
|
- Add stuff you currently have on `/etc/nixos/` to `nixos` (usually
|
|
|
|
`configuration.nix` and `hardware-configuration.nix`, when you're starting
|
|
|
|
out).
|
|
|
|
- The included file has some options you might want, specially if you don't
|
|
|
|
have a configuration ready. Make sure you have generated your own
|
|
|
|
`hardware-configuration.nix`; if not, just mount your partitions to
|
|
|
|
`/mnt` and run: `nixos-generate-config --root /mnt`.
|
|
|
|
- If you're already using home-manager, add your stuff from `~/.config/nixpkgs`
|
|
|
|
to `home-manager` (probably `home.nix`).
|
|
|
|
- I also include one with some simple options if you need. Feel free to
|
|
|
|
ignore this step if you don't want to use home-manager just yet.
|
|
|
|
- Take a look at `flake.nix`, making sure to fill out anything marked with
|
|
|
|
FIXME (required) or TODO (usually tips or optional stuff you might want)
|
2022-04-24 17:03:04 -07:00
|
|
|
- Push your changes! Or at least copy them somewhere if you're on a live
|
|
|
|
medium.
|
2021-12-16 14:12:15 -08:00
|
|
|
|
2022-04-24 16:43:29 -07:00
|
|
|
## Bootstrapping
|
|
|
|
|
2022-04-24 16:35:26 -07:00
|
|
|
To get everything up and running, you need flake-enabled nix and home-manager.
|
|
|
|
|
|
|
|
First off, check your nix version using `nix --version`.
|
|
|
|
|
2022-04-24 16:43:29 -07:00
|
|
|
### Version < 2.4
|
|
|
|
|
2022-04-24 16:35:26 -07:00
|
|
|
Bummer, your nix version does not support flakes. But fear not!
|
|
|
|
|
|
|
|
You _don't_ need to mess around with channels to get up and running.
|
|
|
|
|
|
|
|
Just run:
|
|
|
|
```bash
|
|
|
|
nix-shell
|
|
|
|
```
|
|
|
|
|
|
|
|
Wow, that was easy. Our `shell.nix` file will detect you can't evaluate (nor
|
|
|
|
lock) the flake, and will grab `nix` and `home-manager` from the latest
|
|
|
|
`nixos-unstable` versions, just for you.
|
|
|
|
|
|
|
|
Once you're bootstrapped, remember to run `nix flake lock` and commit the
|
|
|
|
newly-created `flake.lock` into your repository, this will make future
|
|
|
|
bootstraps reproductible.
|
|
|
|
|
2022-04-24 16:43:29 -07:00
|
|
|
### Version >= 2.4
|
|
|
|
|
2022-04-24 16:35:26 -07:00
|
|
|
Congrats, your nix version supports flakes! It's just hidden behind a feature flag.
|
|
|
|
|
|
|
|
You can bootstrap with:
|
|
|
|
```bash
|
2022-04-24 16:56:56 -07:00
|
|
|
nix --experimental-features develop "nix-command flakes" .#default
|
2022-04-24 16:35:26 -07:00
|
|
|
```
|
|
|
|
|
2022-04-24 16:56:56 -07:00
|
|
|
The shell will also enable those experimental features, so no need to pass that
|
|
|
|
argument while you're inside (or after installation, if you set the option that
|
|
|
|
enables them globally).
|
2022-04-24 16:43:29 -07:00
|
|
|
|
2022-04-24 16:35:26 -07:00
|
|
|
This should generate a `flake.lock`. Remember to commit it, as this will make
|
|
|
|
future bootstraps reproductible.
|
|
|
|
|
2022-04-24 16:43:29 -07:00
|
|
|
## Usage
|
2022-04-24 16:35:26 -07:00
|
|
|
|
|
|
|
- Run `sudo nixos-rebuild switch --flake .#hostname` to apply your system
|
|
|
|
configuration.
|
|
|
|
- If you're still on a live installation medium, run `nixos-install --flake
|
2022-04-24 17:28:01 -07:00
|
|
|
.#hostname` instead, and reboot.
|
2022-04-24 16:35:26 -07:00
|
|
|
- Run `home-manager switch --flake .#username@hostname` to apply your home
|
|
|
|
configuration.
|
|
|
|
|
|
|
|
And that's it, really! You're ready to have fun with your configurations using
|
|
|
|
the latest and greatest nix3 flake-enabled command UX.
|
|
|
|
|
2022-04-24 16:43:29 -07:00
|
|
|
# What next?
|
2022-04-24 16:35:26 -07:00
|
|
|
|
2022-04-24 16:43:29 -07:00
|
|
|
## User password and secrets
|
2022-04-24 16:35:26 -07:00
|
|
|
|
2022-04-25 06:03:15 -07:00
|
|
|
You have basically two ways of setting up default passwords:
|
|
|
|
- By default, you'll be prompted for a root password when installing with
|
|
|
|
`nixos-install`. After you reboot, be sure to add a password to your own
|
|
|
|
account and lock root using `sudo passwd -l root`.
|
|
|
|
- Alternatively, you can specify `initialPassword` for your user. This will
|
|
|
|
give your account a default password, be sure to change it after rebooting!
|
|
|
|
If you do, you should pass `--no-root-passwd` to `nixos-install`, to skip
|
|
|
|
setting a password on the root account.
|
2022-04-24 16:35:26 -07:00
|
|
|
|
|
|
|
If you don't want to set your password imperatively, you can also use
|
|
|
|
`passwordFile` for safely and declaratively setting a password from a file
|
|
|
|
outside the nix store.
|
|
|
|
|
2022-04-24 17:07:40 -07:00
|
|
|
There's also [more advanced options for secret
|
|
|
|
management](https://nixos.wiki/wiki/Comparison_of_secret_managing_schemes),
|
|
|
|
including some that can include them (encrypted) into your config repo and/or
|
|
|
|
nix store, be sure to check them out if you're interested.
|
2022-04-24 16:35:26 -07:00
|
|
|
|
2022-04-24 16:56:56 -07:00
|
|
|
## Dotfile management with home-manager
|
2022-04-24 16:35:26 -07:00
|
|
|
|
|
|
|
Besides just adding packages to your environment, home-manager can also manage
|
|
|
|
your dotfiles. I strongly recommend you do, it's awesome!
|
|
|
|
|
2022-04-24 16:56:56 -07:00
|
|
|
For full nix goodness, check out the home-manager options with `man
|
2022-04-24 16:35:26 -07:00
|
|
|
home-configuration.nix`. Using them, you'll be able to fully configure any
|
2022-04-24 16:56:56 -07:00
|
|
|
program with nix syntax and its powerful abstractions.
|
2022-04-24 16:35:26 -07:00
|
|
|
|
2022-04-24 16:56:56 -07:00
|
|
|
Alternatively, if you're still not ready to rewrite all your configs to nix
|
2022-04-24 16:35:26 -07:00
|
|
|
syntax, there's home-manager options (such as `xdg.configFile`) for including
|
2022-04-24 16:56:56 -07:00
|
|
|
files from your config repository into your usual dot directories. Add your
|
2022-04-24 16:35:26 -07:00
|
|
|
existing dotfiles to this repo and try it out!
|
|
|
|
|
2022-04-24 16:43:29 -07:00
|
|
|
## Try opt-in persistance
|
2022-04-24 16:35:26 -07:00
|
|
|
|
|
|
|
You might have noticed that there's impurity in your NixOS system, in the form
|
|
|
|
of configuration files and other cruft your system generates when running. What
|
2022-04-24 16:56:56 -07:00
|
|
|
if you change them in a whim to get something working and forget about it?
|
|
|
|
Boom, your system is not fully reproductible anymore.
|
2022-04-24 16:35:26 -07:00
|
|
|
|
|
|
|
You can instead fully delete your `/` and `/home` on every boot! Nix is okay
|
2022-04-24 17:07:40 -07:00
|
|
|
with a empty root on boot (all you need is `/boot` and `/nix`), and will
|
|
|
|
happily reapply your configurations.
|
2022-04-24 16:35:26 -07:00
|
|
|
|
|
|
|
There's two main approaches to this: mount a `tmpfs` (RAM disk) to `/`, or
|
|
|
|
(using a filesystem such as btrfs or zfs) mount a blank snapshot and reset it
|
|
|
|
on boot.
|
|
|
|
|
|
|
|
For stuff that can't be managed through nix (such as games downloaded from
|
|
|
|
steam, or logs), use [impermanence](https://github.com/nix-community/impermanence)
|
|
|
|
for mounting stuff you to keep to a separate partition/volume (such as
|
|
|
|
`/nix/persist` or `/persist`). This makes everything vanish by default, and you
|
|
|
|
can keep track of what you specifically asked to be kept.
|
2021-12-17 06:32:24 -08:00
|
|
|
|
2022-04-24 17:07:40 -07:00
|
|
|
Here's some awesome blog posts about it:
|
|
|
|
- [Erase your darlings](https://grahamc.com/blog/erase-your-darlings)
|
|
|
|
- [Encrypted BTRFS with Opt-In State on
|
|
|
|
NixOS](https://mt-caret.github.io/blog/posts/2020-06-29-optin-state.html)
|
|
|
|
- [NixOS: tmpfs as root](https://elis.nu/blog/2020/05/nixos-tmpfs-as-root/) and
|
|
|
|
[tmpfs as home](https://elis.nu/blog/2020/06/nixos-tmpfs-as-home/)
|
2021-12-17 06:32:24 -08:00
|
|
|
|
2022-04-24 16:56:56 -07:00
|
|
|
## Adding custom packages
|
2021-12-17 06:32:24 -08:00
|
|
|
|
2022-04-24 16:56:56 -07:00
|
|
|
Something you want to use that's not in nixpkgs yet? You can easily build and
|
|
|
|
iterate on a derivation (package) from this very repository.
|
2021-12-17 06:32:24 -08:00
|
|
|
|
2022-04-24 16:56:56 -07:00
|
|
|
Create a folder with the desired name inside `pkgs`, and add a `default.nix`
|
2022-04-24 16:35:26 -07:00
|
|
|
file containing a derivation. Be sure to also `callPackage` them on
|
|
|
|
`pkgs/default.nix`.
|
2021-12-17 06:32:24 -08:00
|
|
|
|
2022-04-24 16:56:56 -07:00
|
|
|
You'll be able to refer to that package from anywhere on your
|
|
|
|
home-manager/nixos configurations, build them with `nix build .#package-name`,
|
|
|
|
or bring them into your shell with `nix shell .#package-name`.
|
|
|
|
|
|
|
|
See [the manual](https://nixos.org/manual/nixpkgs/stable/) for some tips on how
|
|
|
|
to package stuff.
|
2021-12-17 06:32:24 -08:00
|
|
|
|
2022-04-24 16:43:29 -07:00
|
|
|
## Adding overlays
|
2021-12-17 06:32:24 -08:00
|
|
|
|
2022-04-24 16:56:56 -07:00
|
|
|
Found some outdated package on nixpkgs you need the latest version of? Perhaps
|
|
|
|
you want to apply a patch to fix a behaviour you don't like? Nix makes it easy
|
|
|
|
and manageble with overlays!
|
|
|
|
|
|
|
|
Use the `overlay/default.nix` file for this.
|
2021-12-17 06:32:24 -08:00
|
|
|
|
2022-04-24 16:56:56 -07:00
|
|
|
If you're creating patches, you can keep them on the `overlay` folder as well.
|
2021-12-17 06:32:24 -08:00
|
|
|
|
2022-04-24 16:56:56 -07:00
|
|
|
See [the wiki article](https://nixos.wiki/wiki/Overlays) to see how it all
|
|
|
|
works.
|
2021-12-17 06:32:24 -08:00
|
|
|
|
2022-04-24 16:43:29 -07:00
|
|
|
## Adding your own modules
|
2021-12-17 06:32:24 -08:00
|
|
|
|
2022-04-24 16:56:56 -07:00
|
|
|
Got some configurations you want to create an abstraction of? Modules are the
|
|
|
|
answer. These awesome files can expose _options_ and implement _configurations_
|
|
|
|
based on how the options are set.
|
|
|
|
|
2022-04-24 16:35:26 -07:00
|
|
|
Create a file for them on either `modules/nixos` or `modules/home-manager`. Be
|
|
|
|
sure to also add them to the listing at `modules/nixos/default.nix` or
|
|
|
|
`modules/home-manager/default.nix`.
|
2021-12-17 06:32:24 -08:00
|
|
|
|
2022-04-24 16:56:56 -07:00
|
|
|
See [the wiki article](https://nixos.wiki/wiki/Module) to learn more about
|
|
|
|
them.
|
2021-12-17 06:32:24 -08:00
|
|
|
|
2022-04-24 16:43:29 -07:00
|
|
|
## Adding more hosts or users
|
2022-04-24 16:56:56 -07:00
|
|
|
|
2022-04-24 16:35:26 -07:00
|
|
|
You can organize them by hostname and username on `nixos` and `home-manager`
|
2022-04-24 16:56:56 -07:00
|
|
|
directories, be sure to also add them to `flake.nix`.
|
|
|
|
|
|
|
|
NixOS makes it easy to share common configuration between hosts (you might want
|
|
|
|
to create a common directory for these), while keeping everything in sync.
|
|
|
|
home-manager can help you sync your environment (from editor to WM and
|
|
|
|
everything in between) anywhere you use it. Have fun!
|