Skip to content

Commit

Permalink
Convention-over-configuration layout (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
srid authored Sep 28, 2024
1 parent e5ca39a commit dd31fa1
Show file tree
Hide file tree
Showing 141 changed files with 359 additions and 310 deletions.
5 changes: 4 additions & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
use flake
watch_file \
flake-parts/devshell.nix \
flake-module.nix
use flake
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
result
.direnv
*.qcow2
/result
/.direnv
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ To use this repository as base configuration for your new machine running:
- X1 Carbon: https://srid.ca/x1c7-install
- Windows (via WSL): https://github.com/nix-community/NixOS-WSL
- Clone this repo anywhere
- Edit `flake.nix` to use your system hostname as a key of the `nixosConfigurations` set
- Edit `users/config.nix` to contain your users
- Rename `./modules/nixos/??.nix` to match your current system hostname
- Edit `config.nix` to set your primary user information
- Run `nix run`. That's it. Re-open your terminal.

### macOS

- [Install Nix](https://nixos.asia/en/install)
- Clone this repo anywhere
- Edit `flake.nix` to use your system hostname as a key of the `darwinConfigurations` set
- Edit `users/config.nix` to contain your users
- Rename `./modules/darwin/??.nix` to match your current system hostname
- Edit `config.nix` to set your primary user information
- Run `nix run`.[^cleanup] That's it. Re-open your terminal.

[^cleanup]: You might have to `rm -rf /etc/nix/nix.conf`, so our flake.nix can do its thing.
Expand All @@ -45,11 +45,14 @@ Start from `flake.nix` (see [Flakes](https://nixos.wiki/wiki/Flakes)). [`flake-p

### Directory layout

- `home`: home-manager config (shared between Linux and macOS)
- `nixos`: nixos modules for Linux
- `nix-darwin`: nix-darwin modules for macOS
- `users`: user information
- `systems`: top-level configuration.nix('ish) for various systems
>[!TIP]
> See `flake-module.nix` for autowiring of flake outputs based on this directory structure.
- `configurations`: top-level `flake.{}Configurations` for various systems (`nixos`, `darwin`, `home`)
- `modules`: top-level `flake.{}Modiules` for various systems (`nixos`, `darwin`, `home`, `flake-parts`)
- `overlays`: Overlays
- `packages`: Packages
- `secrets`: agenix secrets configuration

## Tips

Expand Down
12 changes: 12 additions & 0 deletions config.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Configuration for this repo
# See ./modules/flake-parts/config.nix for module options.
{
me = {
username = "srid";
fullname = "Sridhar Ratnakumar";
email = "[email protected]";
# Legacy
# "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCYQ003p7fB5ICQehLwhDBomY9WzkNBeijkSw9ADGU+ECrPakeIH3pntUWRJH1W93vKnLqpkn6HLGEXD9MCR0s98uhh8hT7uAYCxQTbEeKT3PYkfz3oe7XaR8rE601sds0ZyFwH7l8cvK97pGr+uhFXAaohiV6VqmLVXhManEjZZ8GfYWBD9BCmIJk43G3OGa5QYFeHqztprXaJNU5dFPv2Uq2C+L6EvfCfkK2OO1BLZgL+Rai5jjyy6k0fcfsxxd9BdGUwqDhcBeyTIzX9rePMugf/xD+6uNRxTU+vjVpGUtFOw6rpgmVyFv9mn3QMNdQBc5hYKVbIQwMNGTzGgcQv srid@nixos"
sshKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHQRxPoqlThDrkR58pKnJgmeWPY9/wleReRbZ2MOZRyd";
};
}
9 changes: 4 additions & 5 deletions systems/darwin.nix → configurations/darwin/appreciate.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Configuration for my M1 Macbook Max (using nix-darwin)
{ flake, ... }:

let
Expand All @@ -7,8 +8,7 @@ in
{
imports = [
self.darwinModules.default
"${self}/nix-darwin/zsh-completion-fix.nix"
"${self}/nixos/github-runner.nix"
"${self}/modules/nixos/shared/github-runner.nix"
];

nixpkgs.hostPlatform = "aarch64-darwin";
Expand All @@ -17,9 +17,8 @@ in
security.pam.enableSudoTouchIdAuth = true;

# For home-manager to work.
users.users.${flake.config.people.myself} = {
name = flake.config.people.myself;
home = "/Users/${flake.config.people.myself}";
users.users.${flake.config.me.username} = {
home = "/Users/${flake.config.me.username}";
};

system.keyboard = {
Expand Down
13 changes: 13 additions & 0 deletions configurations/home/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# My Ubuntu VM
{ flake, ... }:
let
inherit (flake.inputs) self;
in
{
imports = [
self.homeModules.default
self.homeModules.linux-only
];
home.username = "srid";
home.homeDirectory = "/home/srid";
}
15 changes: 7 additions & 8 deletions systems/ax41.nix → configurations/nixos/immediacy.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Hetzner dedicated: AX41-NVMe
{ flake, ... }:

let
Expand All @@ -8,14 +9,12 @@ in
imports = [
inputs.disko.nixosModules.disko
self.nixosModules.default
"${self}/nixos/disko/trivial.nix"
"${self}/nixos/nix.nix"
"${self}/nixos/self/primary-as-admin.nix"
"${self}/nixos/docker.nix"
"${self}/nixos/actualism-app.nix"
"${self}/nixos/hedgedoc.nix"
"${self}/nixos/github-runner.nix"
"${self}/nixos/server/harden/basics.nix"
"${self}/modules/nixos/linux/disko/trivial.nix"
"${self}/modules/nixos/linux/docker.nix"
"${self}/modules/nixos/linux/actualism-app.nix"
"${self}/modules/nixos/linux/hedgedoc.nix"
"${self}/modules/nixos/linux/server/harden/basics.nix"
"${self}/modules/nixos/shared/github-runner.nix"
];

nixos-flake.sshTarget = "srid@immediacy";
Expand Down
52 changes: 52 additions & 0 deletions flake-module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# An opinionated module that creates flake outputs based on a known directory structure.
#
# cf. Convention over configuration
#
# TODO: Upstream this in some fashion. To srid/nixos-flake?
# cf. https://github.com/juspay/nix-dev-home/issues/86
{ inputs, self, ... }:
let
inherit (inputs.nixpkgs) lib;
forAllNixFiles = dir: f:
lib.pipe dir [
builtins.readDir
(lib.filterAttrs (_: type: type == "regular"))
(lib.mapAttrs' (fn: _:
let name = lib.removeSuffix ".nix" fn; in
lib.nameValuePair name (f "${dir}/${fn}")
))
];
in
{
flake = {
darwinConfigurations =
forAllNixFiles "${self}/configurations/darwin"
(fn: self.nixos-flake.lib.mkMacosSystem { home-manager = true; } fn);

nixosConfigurations =
forAllNixFiles "${self}/configurations/nixos"
(fn: self.nixos-flake.lib.mkLinuxSystem { home-manager = true; } fn);

darwinModules =
forAllNixFiles "${self}/modules/darwin"
(fn: fn);

nixosModules =
forAllNixFiles "${self}/modules/nixos"
(fn: fn);

homeModules =
forAllNixFiles "${self}/modules/home"
(fn: fn);

overlays =
forAllNixFiles "${self}/overlays"
(fn: import fn self.nixos-flake.lib.specialArgsFor.common);
};

perSystem = { pkgs, ... }: {
legacyPackages.homeConfigurations =
forAllNixFiles "${self}/configurations/home"
(fn: self.nixos-flake.lib.mkHomeConfiguration pkgs fn);
};
}
80 changes: 10 additions & 70 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,81 +36,21 @@
outputs = inputs@{ self, ... }:
inputs.flake-parts.lib.mkFlake { inherit inputs; } {
systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" ];
imports = [
inputs.treefmt-nix.flakeModule
inputs.nixos-flake.flakeModule
inputs.nixos-flake.flakeModule
./users
./home
./nixos
./nix-darwin
imports = (with builtins;
map
(fn: ./modules/flake-parts/${fn})
(attrNames (readDir ./modules/flake-parts))) ++
[
./flake-module.nix
];


flake = {
# Configuration for my M1 Macbook Max (using nix-darwin)
darwinConfigurations.appreciate =
self.nixos-flake.lib.mkMacosSystem
{ home-manager = true; }
./systems/darwin.nix;

# Hetzner dedicated
nixosConfigurations.immediacy =
self.nixos-flake.lib.mkLinuxSystem
{ home-manager = true; }
./systems/ax41.nix;
};

perSystem = { self', inputs', pkgs, system, config, ... }: {
# My Ubuntu VM
legacyPackages.homeConfigurations."srid@ubuntu" =
self.nixos-flake.lib.mkHomeConfiguration pkgs {
imports = [
self.homeModules.common-linux
];
home.username = "srid";
home.homeDirectory = "/home/srid";
};

# Flake inputs we want to update periodically
# Run: `nix run .#update`.
nixos-flake = {
primary-inputs = [
"nixpkgs"
"home-manager"
"nix-darwin"
"nixos-flake"
"nix-index-database"
"nixvim"
"omnix"
];
};

treefmt.config = {
projectRootFile = "flake.nix";
programs.nixpkgs-fmt.enable = true;
};

packages.default = self'.packages.activate;

devShells.default = pkgs.mkShell {
name = "nixos-config-shell";
meta.description = "Dev environment for nixos-config";
inputsFrom = [ config.treefmt.build.devShell ];
packages = with pkgs; [
just
colmena
nixd
inputs'.ragenix.packages.default
];
};
perSystem = { self', pkgs, lib, system, ... }: {
# Make our overlay available to the devShell
# "Flake parts does not yet come with an endorsed module that initializes the pkgs argument.""
# So we must do this manually; https://flake.parts/overlays#consuming-an-overlay
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
overlays = [
inputs.nuenv.overlays.default
(import ./packages/overlay.nix { inherit system; flake = { inherit inputs; }; })
];
overlays = lib.attrValues self.overlays;
};
};
};
Expand Down
45 changes: 0 additions & 45 deletions home/default.nix

This file was deleted.

File renamed without changes.
20 changes: 20 additions & 0 deletions modules/darwin/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Configuration common to all macOS systems
{ flake, ... }:
let
inherit (flake) config inputs;
inherit (inputs) self;
in
{
imports = [
{
home-manager.users.${config.me.username} = { };
home-manager.sharedModules = [
self.homeModules.default
self.homeModules.darwin-only
];
}
self.nixosModules.common
inputs.ragenix.darwinModules.default
./all/zsh-completion-fix.nix
];
}
35 changes: 35 additions & 0 deletions modules/flake-parts/config.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Top-level configuration for everything in this repo.
#
# Values are set in 'config.nix' in repo root.
{ lib, ... }:
let
userSubmodule = lib.types.submodule {
options = {
username = lib.mkOption {
type = lib.types.str;
};
fullname = lib.mkOption {
type = lib.types.str;
};
email = lib.mkOption {
type = lib.types.str;
};
sshKey = lib.mkOption {
type = lib.types.str;
description = ''
SSH public key
'';
};
};
};
in
{
imports = [
../../config.nix
];
options = {
me = lib.mkOption {
type = userSubmodule;
};
};
}
Loading

0 comments on commit dd31fa1

Please sign in to comment.