mirror of
https://github.com/michaelthomson0797/nixos-server.git
synced 2025-12-19 03:18:49 +00:00
initial commit
This commit is contained in:
9
README.md
Normal file
9
README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# Nixos Server
|
||||
- This is my nix config to initialize the nodes on my home k3s server.
|
||||
|
||||
```
|
||||
|
||||
nix run github:nix-community/nixos-anywhere -- --build-on-remote --generate-hardware-config nixos-generate-config ./hardware-configuration.nix --flake .#patrick root@192.168.2.100
|
||||
|
||||
nix run github:nix-community/nixos-anywhere -- --build-on-remote --generate-hardware-config nixos-generate-config ./hardware-configuration.nix --flake .#spongebob root@192.168.2.101
|
||||
```
|
||||
48
flake.lock
generated
Normal file
48
flake.lock
generated
Normal file
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"nodes": {
|
||||
"disko": {
|
||||
"inputs": {
|
||||
"nixpkgs": [
|
||||
"nixpkgs"
|
||||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1749147380,
|
||||
"narHash": "sha256-UvCI5f1qD9l1fCQkoG/kJI0yNjDQIiJaN7gkve8fmII=",
|
||||
"owner": "nix-community",
|
||||
"repo": "disko",
|
||||
"rev": "d74db625a5cf3f46cf8fa545d6ef10bd3463ea07",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-community",
|
||||
"repo": "disko",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1748856973,
|
||||
"narHash": "sha256-RlTsJUvvr8ErjPBsiwrGbbHYW8XbB/oek0Gi78XdWKg=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "e4b09e47ace7d87de083786b404bf232eb6c89d8",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixpkgs-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"disko": "disko",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
47
flake.nix
Normal file
47
flake.nix
Normal file
@@ -0,0 +1,47 @@
|
||||
{
|
||||
description = "Michael's NixOS Homelab Flake";
|
||||
|
||||
inputs = {
|
||||
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
||||
disko = {
|
||||
url = "github:nix-community/disko";
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
};
|
||||
|
||||
outputs = inputs @ {
|
||||
nixpkgs,
|
||||
disko,
|
||||
...
|
||||
}: let
|
||||
nodes = [
|
||||
"patrick"
|
||||
"spongebob"
|
||||
"larry"
|
||||
];
|
||||
k3sToken = "FILL THIS IN";
|
||||
publicKey = "FILL THIS IN";
|
||||
serverAddr = "https://192.168.2.100:6443";
|
||||
in {
|
||||
nixosConfigurations = builtins.listToAttrs (map (name: {
|
||||
name = name;
|
||||
value = nixpkgs.lib.nixosSystem {
|
||||
specialArgs = {
|
||||
inherit inputs;
|
||||
meta = {
|
||||
hostname = name;
|
||||
k3sToken = k3sToken;
|
||||
publicKey = publicKey;
|
||||
serverAddr = serverAddr;
|
||||
};
|
||||
};
|
||||
system = "x86_64-linux";
|
||||
modules = [
|
||||
disko.nixosModules.disko
|
||||
./hosts/${name}/configuration.nix
|
||||
];
|
||||
};
|
||||
})
|
||||
nodes);
|
||||
};
|
||||
}
|
||||
16
hosts/larry/configuration.nix
Normal file
16
hosts/larry/configuration.nix
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
meta,
|
||||
...
|
||||
}: {
|
||||
imports = [
|
||||
./disk-config.nix
|
||||
../../modules/base.nix
|
||||
../../modules/k3s.nix
|
||||
../../modules/nfs.nix
|
||||
../../modules/longhorn.nix
|
||||
../../modules/nvidia.nix
|
||||
];
|
||||
}
|
||||
33
hosts/larry/disk-config.nix
Normal file
33
hosts/larry/disk-config.nix
Normal file
@@ -0,0 +1,33 @@
|
||||
{...}: {
|
||||
disko.devices = {
|
||||
disk = {
|
||||
main = {
|
||||
device = "/dev/nvme0n1";
|
||||
type = "disk";
|
||||
content = {
|
||||
type = "gpt";
|
||||
partitions = {
|
||||
ESP = {
|
||||
type = "EF00";
|
||||
size = "500M";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "vfat";
|
||||
mountpoint = "/boot";
|
||||
mountOptions = ["umask=0077"];
|
||||
};
|
||||
};
|
||||
root = {
|
||||
size = "100%";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "ext4";
|
||||
mountpoint = "/";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
19
hosts/patrick/configuration.nix
Normal file
19
hosts/patrick/configuration.nix
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
meta,
|
||||
...
|
||||
}: {
|
||||
imports = [
|
||||
./disk-config.nix
|
||||
../../modules/base.nix
|
||||
../../modules/k3s.nix
|
||||
../../modules/nfs.nix
|
||||
../../modules/longhorn.nix
|
||||
];
|
||||
|
||||
k3s = {
|
||||
init = true;
|
||||
};
|
||||
}
|
||||
33
hosts/patrick/disk-config.nix
Normal file
33
hosts/patrick/disk-config.nix
Normal file
@@ -0,0 +1,33 @@
|
||||
{...}: {
|
||||
disko.devices = {
|
||||
disk = {
|
||||
main = {
|
||||
device = "/dev/sda";
|
||||
type = "disk";
|
||||
content = {
|
||||
type = "gpt";
|
||||
partitions = {
|
||||
ESP = {
|
||||
type = "EF00";
|
||||
size = "500M";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "vfat";
|
||||
mountpoint = "/boot";
|
||||
mountOptions = ["umask=0077"];
|
||||
};
|
||||
};
|
||||
root = {
|
||||
size = "100%";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "ext4";
|
||||
mountpoint = "/";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
15
hosts/spongebob/configuration.nix
Normal file
15
hosts/spongebob/configuration.nix
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
meta,
|
||||
...
|
||||
}: {
|
||||
imports = [
|
||||
./disk-config.nix
|
||||
../../modules/base.nix
|
||||
../../modules/k3s.nix
|
||||
../../modules/nfs.nix
|
||||
../../modules/longhorn.nix
|
||||
];
|
||||
}
|
||||
33
hosts/spongebob/disk-config.nix
Normal file
33
hosts/spongebob/disk-config.nix
Normal file
@@ -0,0 +1,33 @@
|
||||
{...}: {
|
||||
disko.devices = {
|
||||
disk = {
|
||||
main = {
|
||||
device = "/dev/sda";
|
||||
type = "disk";
|
||||
content = {
|
||||
type = "gpt";
|
||||
partitions = {
|
||||
ESP = {
|
||||
type = "EF00";
|
||||
size = "500M";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "vfat";
|
||||
mountpoint = "/boot";
|
||||
mountOptions = ["umask=0077"];
|
||||
};
|
||||
};
|
||||
root = {
|
||||
size = "100%";
|
||||
content = {
|
||||
type = "filesystem";
|
||||
format = "ext4";
|
||||
mountpoint = "/";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
56
modules/base.nix
Normal file
56
modules/base.nix
Normal file
@@ -0,0 +1,56 @@
|
||||
{
|
||||
pkgs,
|
||||
meta,
|
||||
...
|
||||
}: {
|
||||
nix.settings.experimental-features = ["nix-command" "flakes"];
|
||||
|
||||
nixpkgs.config.allowUnfree = true;
|
||||
|
||||
time.timeZone = "America/Toronto";
|
||||
|
||||
boot = {
|
||||
loader = {
|
||||
systemd-boot.enable = true;
|
||||
efi.canTouchEfiVariables = true;
|
||||
};
|
||||
};
|
||||
|
||||
networking = {
|
||||
hostName = meta.hostname;
|
||||
networkmanager.enable = true;
|
||||
firewall.enable = false;
|
||||
};
|
||||
|
||||
environment.systemPackages = with pkgs; [
|
||||
curl
|
||||
vim
|
||||
git
|
||||
];
|
||||
|
||||
users = {
|
||||
mutableUsers = false;
|
||||
users = {
|
||||
mthomson = {
|
||||
isNormalUser = true;
|
||||
extraGroups = ["wheel"];
|
||||
password = "pw123";
|
||||
openssh.authorizedKeys.keys = [meta.publicKey];
|
||||
};
|
||||
root = {
|
||||
openssh.authorizedKeys.keys = [meta.publicKey];
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
services = {
|
||||
openssh = {
|
||||
enable = true;
|
||||
settings = {
|
||||
PasswordAuthentication = false;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
system.stateVersion = "25.05";
|
||||
}
|
||||
49
modules/k3s.nix
Normal file
49
modules/k3s.nix
Normal file
@@ -0,0 +1,49 @@
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
meta,
|
||||
...
|
||||
}:
|
||||
with lib; let
|
||||
cfg = config.k3s;
|
||||
in {
|
||||
options.k3s = {
|
||||
init = mkOption {
|
||||
type = types.bool;
|
||||
default = false;
|
||||
description = "Initialize cluster when set to true";
|
||||
};
|
||||
};
|
||||
|
||||
config = {
|
||||
services = {
|
||||
k3s = {
|
||||
enable = true;
|
||||
role = "server";
|
||||
clusterInit = cfg.init;
|
||||
serverAddr =
|
||||
if cfg.init
|
||||
then ""
|
||||
else meta.serverAddr;
|
||||
token =
|
||||
if cfg.init
|
||||
then ""
|
||||
else meta.k3sToken;
|
||||
extraFlags =
|
||||
[
|
||||
"--kubelet-arg=allowed-unsafe-sysctls=net.ipv4.*"
|
||||
]
|
||||
++ (
|
||||
if cfg.init
|
||||
then [
|
||||
"--disable servicelb"
|
||||
"--disable traefik"
|
||||
"--disable local-storage"
|
||||
"--write-kubeconfig-mode \"0644\""
|
||||
]
|
||||
else []
|
||||
);
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
15
modules/longhorn.nix
Normal file
15
modules/longhorn.nix
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
config,
|
||||
pkgs,
|
||||
...
|
||||
}: {
|
||||
environment.systemPackages = [pkgs.nfs-utils];
|
||||
services.openiscsi = {
|
||||
enable = true;
|
||||
name = "${config.networking.hostName}-initiatorhost";
|
||||
};
|
||||
# Fixes for longhorn path mapping
|
||||
systemd.tmpfiles.rules = [
|
||||
"L+ /usr/local/bin - - - - /run/current-system/sw/bin/"
|
||||
];
|
||||
}
|
||||
4
modules/nfs.nix
Normal file
4
modules/nfs.nix
Normal file
@@ -0,0 +1,4 @@
|
||||
{...}: {
|
||||
boot.supportedFilesystems = ["nfs"];
|
||||
services.rpcbind.enable = true;
|
||||
}
|
||||
33
modules/nvidia.nix
Normal file
33
modules/nvidia.nix
Normal file
@@ -0,0 +1,33 @@
|
||||
{
|
||||
config,
|
||||
pkgs,
|
||||
...
|
||||
}: {
|
||||
hardware.nvidia = {
|
||||
open = true;
|
||||
package = config.boot.kernelPackages.nvidiaPackages.stable;
|
||||
nvidiaSettings = true;
|
||||
};
|
||||
|
||||
services.xserver = {
|
||||
enable = false;
|
||||
videoDrivers = ["nvidia"];
|
||||
};
|
||||
|
||||
hardware.nvidia-container-toolkit.enable = true;
|
||||
hardware.nvidia-container-toolkit.mount-nvidia-executables = true;
|
||||
|
||||
environment.systemPackages = with pkgs; [
|
||||
nvidia-container-toolkit
|
||||
];
|
||||
|
||||
services.k3s.containerdConfigTemplate = ''
|
||||
{{ template "base" . }}
|
||||
|
||||
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.nvidia]
|
||||
privileged_without_host_devices = false
|
||||
runtime_engine = ""
|
||||
runtime_root = ""
|
||||
runtime_type = "io.containerd.runc.v2"
|
||||
'';
|
||||
}
|
||||
Reference in New Issue
Block a user