Skip to content

Commit

Permalink
WIP: NixOS
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Oct 4, 2024
1 parent 22ed18c commit 4ffd00a
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 21 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
branches:
- main
- update_flake_lock_action
- nixos
pull_request:
jobs:
nix:
Expand All @@ -18,6 +19,31 @@ jobs:
run: om show .
- name: om ci
run: om ci --extra-access-tokens "github.com=${{ secrets.GITHUB_TOKEN }}" run --systems "${{ matrix.system }}"

# Make sure that the templates at least build after initialized
# TODO: Ideally, we want to let `omnix` run these tests
# https://github.com/juspay/omnix/issues/282
- name: NixOS test
if: matrix.system == 'x86_64-linux'
run: |
om init .#nixos -o /tmp/nixconfig \
--non-interactive \
--params '{"hostname": "example", "username": "srid", "git-name": "Srid", "git-email": "srid@srid.ca"}'
set -x
nix build /tmp/nixconfig#nixosConfigurations.example.config.system.build.toplevel
type -f ./result/etc/profiles/per-user/srid/bin/git
- name: nix-darwin test
if: matrix.system == 'aarch64-darwin' || matrix.system == 'x86_64-darwin'
run: |
om init .#darwin -o /tmp/nixconfig \
--non-interactive \
--params '{"hostname": "example", "username": "srid", "git-name": "Srid", "git-email": "srid@srid.ca"}'
set -x
nix build /tmp/nixconfig#darwinConfigurations.example.config.system.build.toplevel
type -f ./result/etc/profiles/per-user/srid/bin/git
# TODO: Use `om init` like above
- name: activate-test
if: matrix.system == 'x86_64-linux'
run: nix run .#test-home-manager-config
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ We currently support [home-manager] (see `./modules/home`) and [nix-darwin] (see

## Getting Started

### On NixOS

1. Install NixOS from [Graphical ISO image](https://nixos.org/download/#download-nixos) and reboot.
1. Ensure that `/etc/nixos/{configuration.nix, hardware-configuration.nix}` are in place.
1. In a terminal, become `root` and initialize our template under `/etc/nixos`:
```sh-session
sudo su -
cd /etc/nixos
nix --accept-flake-config --extra-experimental-features "nix-command flakes" \
run github:juspay/omnix -- \
init github:juspay/nixos-unified-template#nixos -o .
# Replace HOSTNAME with the hostname you entered above.
mv configuration.nix hardware-configuration.nix ./configurations/nixos/HOSTNAME/
nix --extra-experimental-features "nix-command flakes" run
```
1. At this point, you can move `/etc/nixos` to anywhere, and initialize a Git repository to track future changes.

### On non-NixOS

1. [Install Nix](https://nixos.asia/en/install):
```sh-session
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | \
Expand Down
17 changes: 17 additions & 0 deletions configurations/nixos/example/configuration.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
# These are normally in hardware-configuration.nix
boot.loader.grub.device = "nodev";
fileSystems."/" = { device = "/dev/disk/by-label/nixos"; fsType = "btrfs"; };

nixpkgs.hostPlatform = "x86_64-linux";
networking.hostName = "example";

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

# Used for backwards compatibility, please read the changelog before changing.
# $ darwin-rebuild changelog
system.stateVersion = "24.05";
}
20 changes: 20 additions & 0 deletions configurations/nixos/example/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# See /modules/nixos/* for actual settings
# This file is just *top-level* configuration.
{ flake, ... }:

let
inherit (flake) inputs;
inherit (inputs) self;
in
{
imports = [
self.nixosModules.default
./configuration.nix
];

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

}
72 changes: 55 additions & 17 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 43 additions & 4 deletions modules/flake-parts/template.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@

filters = path: with inputs.nixpkgs.lib; {
homeOnly =
hasInfix "configurations/home" path
|| hasSuffix "activate-home.nix" path;
# NOTE: configurations/home/* is imported in nix-darwin and NixOS
hasSuffix "activate-home.nix" path;
darwinOnly =
hasInfix "configurations/darwin" path
|| hasInfix "modules/darwin" path;
nixosOnly =
hasInfix "configurations/nixos" path
|| hasInfix "modules/nixos" path;
alwaysExclude =
hasSuffix "LICENSE" path
|| hasSuffix "README.md" path
Expand Down Expand Up @@ -48,10 +51,33 @@
path = builtins.path {
path = parent.path;
filter = path: _:
!(filters path).darwinOnly;
let f = filters path;
in
!(f.nixosOnly || f.darwinOnly);
};
};

nixos = let parent = templates.default; in {
description = mkDescription "NixOS";
welcomeText = ''
You have just created a nixos-unified-template flake.nix using NixOS.
- Edit `./modules/nixos/*.nix` to customize your configuration.
- Run `mv /etc/nixos/*.nix ./configurations/nixos/HOSTNAME/` to import your existing configuration.
- Run `nix --extra-experimental-features "nix-command flakes" run` to apply the configuration.
Enjoy!
'';
path = builtins.path {
path = parent.path;
filter = path: _:
let f = filters path;
in
!(f.darwinOnly || f.homeOnly);
};

};

nix-darwin = let parent = templates.default; in {
description = mkDescription "nix-darwin";
welcomeText = ''
Expand All @@ -75,7 +101,9 @@
path = builtins.path {
path = parent.path;
filter = path: _:
!(filters path).homeOnly;
let f = filters path;
in
!(f.nixosOnly || f.homeOnly);
};
};
};
Expand Down Expand Up @@ -111,6 +139,17 @@
];
};

nixos = {
template = templates.nixos;
params = [
{
name = "hostname";
description = "Your system hostname as shown by `hostname -s`";
placeholder = "example";
}
] ++ om.templates.home.params;
};

darwin = {
template = templates.nix-darwin;
params = [
Expand Down
11 changes: 11 additions & 0 deletions modules/nixos/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This is your nix-darwin configuration.
# For home configuration, see /modules/home/*
{ flake, pkgs, lib, ... }:

let
inherit (flake) inputs;
inherit (inputs) self;
in
{
services.openssh.enable = true;
}

0 comments on commit 4ffd00a

Please sign in to comment.