Skip to content

Commit

Permalink
Introduce config.nix, to avoid hardcoding (#125)
Browse files Browse the repository at this point in the history
Add username, full name and email to it.
  • Loading branch information
srid authored Feb 1, 2025
1 parent 9d0f93c commit f9b1116
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 22 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

# nixos-unified-template

A multi-platform Nix configuration template optimized as development environment (includes direnv, neovim with LSP[^neovim] and such), based on [nixos-unified]. See [`./modules`](modules/) to see what's available. We support [home-manager] (see `./modules/home`), [nix-darwin] (see `./modules/darwin`) and [NixOS] (see `./modules/nixos`).
This repository provides a ready-made Nix configuration to manage either your Home environment or whole NixOS systems. It is optimized specifically for development environment (with direnv, neovim with LSP[^neovim], etc.).

Under the hood, it uses [nixos-unified]. See [`./modules`](modules/) to see what's available. We support [home-manager] (see `./modules/home`), [nix-darwin] (see `./modules/darwin`) and [NixOS] (see `./modules/nixos`).

| Platform | Supported By |
|-------------|-------------------------------------------|
Expand Down Expand Up @@ -86,12 +88,12 @@ Whenever you modify your configuration in `./modules/*/*.nix`, you should re-run
The configuration repo has `flake.nix` file in the current directory and some `./modules/{home,darwin,nixos}/*.nix` files containing the [home-manager], [nix-darwin] and [NixOS] configurations respectively that you can review. It also has a [justfile](https://github.com/casey/just), which provides a set of recipes analogous to Make targets to interact with the nix flake.
You can then execute `nix develop`, to ensure you are in the development shell with [just](https://github.com/casey/just) installed, followed by `just run` (or `nix run`) to activate this configuration in your system.
If you prefer, you can simply execute `nix run`, but using `just` will perform some additional validation and ensure you are able to use the other commands in the [justfile](./justfile).
Run `nix run` to activate this configuration in your system.
To browse the capabilities of [home-manager] (and to see what else can go in your `./modules/home/*.nix` -- such as shell aliases), consult [home-manager options reference](https://nix-community.github.io/home-manager/options.xhtml). You can also run `man home-configuration.nix` in the terminal.
Global configuration is in `./modules/flake-parts/config.nix`. Here, you can specify your user name, email and such settings.
## What's included
Here we describe just a handful of tools included in this template. See the [./modules](./modules) directory for more.
Expand Down Expand Up @@ -142,4 +144,3 @@ sudo ln -s /nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt /etc/ssl/ce
```

See https://github.com/NixOS/nix/issues/2899#issuecomment-1669501326

11 changes: 6 additions & 5 deletions configurations/darwin/example.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
let
inherit (flake) inputs;
inherit (inputs) self;
inherit (flake.config) me;
in
{
imports = [
Expand All @@ -16,19 +17,19 @@ in

# For home-manager to work.
# https://github.com/nix-community/home-manager/issues/4026#issuecomment-1565487545
users.users."runner".home = "/Users/runner";
users.users."${me.username}".home = "/Users/${me.username}";

home-manager = {
# Automatically move old dotfiles out of the way
#
# Note that home-manager is not very smart, if this backup file already exists it
# Note that home-manager is not very smart, if this backup file already exists it
# will complain "Existing file .. would be clobbered by backing up". To mitigate this,
# we try to use as unique a backup file extension as possible.
backupFileExtension = "nixos-unified-template-backup";

# Enable home-manager for "runner" user
users."runner" = {
imports = [ (self + /configurations/home/runner.nix) ];
# Enable home-manager for our user
users."${me.username}" = {
imports = [ (self + /configurations/home/${me.username}.nix) ];
};
};

Expand Down
5 changes: 3 additions & 2 deletions configurations/home/runner.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
let
inherit (flake) inputs;
inherit (inputs) self;
inherit (flake.config) me;
in
{
imports = [
Expand All @@ -17,7 +18,7 @@ in
config.nix.package
];

home.username = "runner";
home.homeDirectory = lib.mkDefault "/${if pkgs.stdenv.isDarwin then "Users" else "home"}/runner";
home.username = me.username;
home.homeDirectory = lib.mkDefault "/${if pkgs.stdenv.isDarwin then "Users" else "home"}/${me.username}";
home.stateVersion = "24.11";
}
6 changes: 5 additions & 1 deletion configurations/nixos/example/configuration.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# NOTE: We expect this file to be supplanted by the original /etc/nixos/configuration.nix
{ flake, ... }:
let
inherit (flake.config) me;
in
{
# These are normally in hardware-configuration.nix
boot.loader.grub.device = "nodev";
Expand All @@ -9,7 +13,7 @@

# For home-manager to work.
# https://github.com/nix-community/home-manager/issues/4026#issuecomment-1565487545
users.users."runner".isNormalUser = true;
users.users."${me.username}".isNormalUser = true;

# Used for backwards compatibility, please read the changelog before changing.
# $ darwin-rebuild changelog
Expand Down
7 changes: 4 additions & 3 deletions configurations/nixos/example/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
let
inherit (flake) inputs;
inherit (inputs) self;
inherit (flake.config) me;
in
{
imports = [
Expand All @@ -13,8 +14,8 @@ in
./configuration.nix
];

# Enable home-manager for "runner" user
home-manager.users."runner" = {
imports = [ (self + /configurations/home/runner.nix) ];
# Enable home-manager for our user
home-manager.users."${me.username}" = {
imports = [ (self + /configurations/home/${me.username}.nix) ];
};
}
3 changes: 2 additions & 1 deletion modules/darwin/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
let
inherit (flake) inputs;
inherit (inputs) self;
inherit (flake.config) me;
in
{
# Use TouchID for `sudo` authentication
security.pam.enableSudoTouchIdAuth = true;

# These users can add Nix caches.
nix.settings.trusted-users = [ "root" "runner" ];
nix.settings.trusted-users = [ "root" me.username ];

# Configure macOS system
# More examples => https://github.com/ryan4yin/nix-darwin-kickstarter/blob/main/rich-demo/modules/system.nix
Expand Down
27 changes: 27 additions & 0 deletions modules/flake-parts/config.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{ lib, ... }:
{
options = {
me = lib.mkOption {
default = { };
type = lib.types.submodule {
options = {
username = lib.mkOption {
type = lib.types.str;
default = "runner";
description = "Your username as shown by `id -un`";
};
fullname = lib.mkOption {
type = lib.types.str;
default = "John Doe";
description = "Your full name for use in Git config";
};
email = lib.mkOption {
type = lib.types.str;
default = "[email protected]";
description = "Your email for use in Git config";
};
};
};
};
};
}
9 changes: 6 additions & 3 deletions modules/home/git.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{ ... }:
{ flake, ... }:
let
inherit (flake.config) me;
in
{
home.shellAliases = {
g = "git";
Expand All @@ -9,8 +12,8 @@
programs = {
git = {
enable = true;
userName = "John Doe";
userEmail = "[email protected]";
userName = me.fullname;
userEmail = me.email;
ignores = [ "*~" "*.swp" ];
aliases = {
ci = "commit";
Expand Down
2 changes: 1 addition & 1 deletion modules/home/neovim/nixvim.nix
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
# Open lazygit within nvim.
{
action = "<cmd>LazyGit<CR>";
key = "<leader>gg";
key = "<leader>gg";
}
];
}
3 changes: 2 additions & 1 deletion modules/nixos/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
let
inherit (flake) inputs;
inherit (inputs) self;
inherit (flake.config) me;
in
{
# These users can add Nix caches.
nix.settings.trusted-users = [ "root" "runner" ];
nix.settings.trusted-users = [ "root" me.username ];

services.openssh.enable = true;
}

0 comments on commit f9b1116

Please sign in to comment.