Compare commits

..

4 Commits

Author SHA1 Message Date
Henrik
981683bf51 first succesful deployment of a nixos using nixos-anywhere #4 2025-10-26 15:49:15 +01:00
Henrik
3cd6c1c941 added an ssh key for neo 2025-10-26 15:14:34 +01:00
Henrik
665bd1d1f4 updated disko setup 2025-10-15 20:34:14 +02:00
Henrik
814d54bcf4 A simple flake setup with tests and VM support 2025-10-09 22:31:48 +02:00
13 changed files with 435 additions and 0 deletions

3
.gitignore vendored
View File

@@ -4,3 +4,6 @@ result-*
# Ignore automatically generated direnv output
.direnv
# Ignore VM disk images
*.qcow2

View File

@@ -1,2 +1,16 @@
# matrix
Who needs something else?
# Nixos Setup
## Automatic Tests
Using the nixos framework, tests can be written and performed. See https://nixos.org/manual/nixos/stable/index.html#sec-nixos-tests
Perform all tests using `nix flake check`. Call a specific test using `nix build .#checks.x86_64-linux.test1`
## Testing the VM
The VM can be build using `nixos-rebuild build-vm --flake .#matrix` and started using `result/bin/run-nixos-vm`.
See also https://gist.github.com/FlakM/0535b8aa7efec56906c5ab5e32580adf

86
flake.lock generated Normal file
View File

@@ -0,0 +1,86 @@
{
"nodes": {
"disko": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1758287904,
"narHash": "sha256-IGmaEf3Do8o5Cwp1kXBN1wQmZwQN3NLfq5t4nHtVtcU=",
"owner": "nix-community",
"repo": "disko",
"rev": "67ff9807dd148e704baadbd4fd783b54282ca627",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "disko",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1759735786,
"narHash": "sha256-a0+h02lyP2KwSNrZz4wLJTu9ikujNsTWIC874Bv7IJ0=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "20c4598c84a671783f741e02bf05cbfaf4907cff",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-25.05",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-unstable": {
"locked": {
"lastModified": 1759831965,
"narHash": "sha256-vgPm2xjOmKdZ0xKA6yLXPJpjOtQPHfaZDRtH+47XEBo=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "c9b6fb798541223bbb396d287d16f43520250518",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"disko": "disko",
"nixpkgs": "nixpkgs",
"nixpkgs-unstable": "nixpkgs-unstable",
"sops": "sops"
}
},
"sops": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1759635238,
"narHash": "sha256-UvzKi02LMFP74csFfwLPAZ0mrE7k6EiYaKecplyX9Qk=",
"owner": "Mic92",
"repo": "sops-nix",
"rev": "6e5a38e08a2c31ae687504196a230ae00ea95133",
"type": "github"
},
"original": {
"owner": "Mic92",
"repo": "sops-nix",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

53
flake.nix Normal file
View File

@@ -0,0 +1,53 @@
{
description = "Flake to setup server using matrix communication protocol";
inputs = {
# stable and unstable nixpkgs
nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05";
nixpkgs-unstable.url = "github:nixos/nixpkgs/nixos-unstable";
# partitioning and disk management
disko = {
url = "github:nix-community/disko";
inputs.nixpkgs.follows = "nixpkgs";
};
# secret management
sops = {
url = "github:Mic92/sops-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, nixpkgs-unstable, disko, sops, ... }@inputs: {
nixosConfigurations = {
# TODO replace hostname
matrix = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
disko.nixosModules.disko
sops.nixosModules.sops
./nix/configuration.nix
./nix/disko.nix
];
specialArgs = { inherit inputs; };
};
};
# Add this section
checks.x86_64-linux = {
test1 = import ./nix/tests/test1.nix {
pkgs = nixpkgs.legacyPackages.x86_64-linux;
};
};
# Development shell for working with the configuration
# execute: nix develop or use direnv
devShells.x86_64-linux.default = nixpkgs.legacyPackages.x86_64-linux.mkShell {
buildInputs = with nixpkgs.legacyPackages.x86_64-linux; [
# git
];
};
};
}

102
nix/configuration.nix Normal file
View File

@@ -0,0 +1,102 @@
{ config, pkgs, inputs, lib, ... }:
{
imports = [
./users/users.nix
./modules/security.nix
./vps/hetzner/hardware-configuration.nix
./modules/zsh.nix
];
# nix settings
nix.settings.experimental-features = [ "nix-command" "flakes" ];
# Bootloader to work with LUKS
boot.loader.grub = {
enable = true;
# https://github.com/NixOS/nixpkgs/issues/55332
device = "nodev"; # Don't install to MBR
efiSupport = true; # Enable EFI support
enableCryptodisk = true; # Enable LUKS support
};
boot.loader.efi.canTouchEfiVariables = true;
# LUKS configuration
boot.initrd.luks.devices."crypted" = {
device = "/dev/disk/by-partlabel/luks";
allowDiscards = true;
};
boot.initrd.kernelModules = [ "virtio_gpu" ];
boot.kernelParams = [ "console=tty" ];
networking.hostName = "matrix";
# time zone
time.timeZone = "Europe/Zurich";
# Select internationalisation properties.
i18n.defaultLocale = "en_US.UTF-8";
i18n.extraLocaleSettings = {
LC_ADDRESS = "de_DE.UTF-8";
LC_IDENTIFICATION = "de_DE.UTF-8";
LC_MEASUREMENT = "de_DE.UTF-8";
LC_MONETARY = "de_DE.UTF-8";
LC_NAME = "de_DE.UTF-8";
LC_NUMERIC = "de_DE.UTF-8";
LC_PAPER = "de_DE.UTF-8";
LC_TELEPHONE = "de_DE.UTF-8";
LC_TIME = "de_DE.UTF-8";
};
# Allow unfree packages
# nixpkgs.config.allowUnfree = true;
# List packages installed in system profile. To search, run:
environment.systemPackages = with pkgs; [
];
virtualisation.vmVariant = {
# following configuration is added only when building VM with build-vm
virtualisation = {
memorySize = 4000;
cores = 2;
graphics = false;
diskSize = 5000; # 5GB, needed to prevent docker error running out of space
# Networking configuration
forwardPorts = [
{ from = "host"; host.port = 2222; guest.port = 22; }
];
};
# Add VM-specific users
users.users.smith = {
isNormalUser = true;
description = "VM Test User";
extraGroups = [ "wheel" "networkmanager" ];
shell = pkgs.zsh;
initialPassword = "smith";
packages = with pkgs; [ ];
};
# VM-specific packages
environment.systemPackages = with pkgs; [
];
# in order to build VM on x86_64 host
nixpkgs.hostPlatform = lib.mkForce "x86_64-linux";
};
# This value determines the NixOS release from which the default
# settings for stateful data, like file locations and database versions
# on your system were taken. Its perfectly fine and recommended to leave
# this value at the release version of the first install of this system.
# Before changing this value read the documentation for this option
# (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
system.stateVersion = "25.05"; # Did you read the comment?
}

41
nix/disko.nix Normal file
View File

@@ -0,0 +1,41 @@
# other examples, see https://github.com/nix-community/disko/blob/master/example/
{
disko.devices = {
disk = {
main = {
type = "disk";
device = "/dev/sda";
content = {
type = "gpt";
partitions = {
ESP = {
size = "500M";
type = "EF00";
label = "boot";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
mountOptions = [ "umask=0077" ];
};
};
luks = {
size = "100%";
label = "luks";
content = {
type = "luks";
name = "crypted";
settings.allowDiscards = true; # Enable SSD TRIM support
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/";
};
};
};
};
};
};
};
};
}

17
nix/modules/security.nix Normal file
View File

@@ -0,0 +1,17 @@
{ config, pkgs, inputs, ... }:
{
# providing an ssh configuration
services.openssh = {
enable = true;
settings = {
PermitRootLogin = "no"; # Disable root login
PasswordAuthentication = false; # Force SSH key auth only
PubkeyAuthentication = true; # Enable SSH keys
};
ports = [ 22 ];
};
# other security hardening options can go here
security.sudo.wheelNeedsPassword = false;
}

40
nix/modules/zsh.nix Normal file
View File

@@ -0,0 +1,40 @@
{ lib, config, pkgs, ... }:
{
programs.zsh = {
enable = true;
enableCompletion = true;
autosuggestions.enable = true;
syntaxHighlighting.enable = true;
enableLsColors = true;
histSize = 10000;
histFile = "$HOME/.zsh_history";
shellAliases = {
ll = "ls - l";
};
ohMyZsh = {
enable = true;
plugins = [
"sudo"
#"direnv"
#"fzf"
];
theme = "terminalparty";
};
# custom zsh options
setOptions = [
"HIST_IGNORE_DUPS" # do not write dupes
"HIST_SAVE_NO_DUPS"
"HIST_IGNORE_ALL_DUPS"
"HIST_FIND_NO_DUPS"
"APPEND_HISTORY" # append rather than overwrite ...?
"SHARE_HISTORY" # all zsh sessions share history file
"HIST_FCNTL_LOCK" # useful to prevent lockups ...? see github
"HIST_IGNORE_SPACE" # add space before command to not write to history
];
};
}

25
nix/tests/test1.nix Normal file
View File

@@ -0,0 +1,25 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.nixosTest {
name = "matrix-login-test";
nodes = {
machine = { config, pkgs, ... }: {
imports = [
../configuration.nix
];
# Test configuration
virtualisation.memorySize = 2048;
};
};
testScript = ''
start_all()
machine.wait_for_unit("multi-user.target")
with subtest("SSH service test"):
machine.wait_for_unit("sshd.service")
machine.wait_for_open_port(22)
'';
}

1
nix/users/keys/neo.pub Normal file
View File

@@ -0,0 +1 @@
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBtePfzkSorgiFNuol/pEYlR0HToDCy9fk8PPfZWMuf3 henrik@strange

32
nix/users/users.nix Normal file
View File

@@ -0,0 +1,32 @@
{ config, pkgs, inputs, ... }:
{
# Define user accounts
users.defaultUserShell = pkgs.zsh;
users.users.neo = {
isNormalUser = true;
description = "Neovim only user";
extraGroups = [ "networkmanager" "wheel" ];
shell = pkgs.zsh;
packages = with pkgs; [ ];
openssh.authorizedKeys.keyFiles = [
./keys/neo.pub
];
};
users.users.morpheus = {
isNormalUser = true;
description = "Insert joke here";
extraGroups = [ "networkmanager" "wheel" ];
shell = pkgs.zsh;
packages = with pkgs; [ ];
};
users.users.trinity = {
isNormalUser = true;
description = "Named after an atom bomb test";
extraGroups = [ "networkmanager" "wheel" ];
shell = pkgs.zsh;
packages = with pkgs; [ ];
};
}

View File

@@ -0,0 +1,11 @@
{ config, lib, ... }:
{
# Specific settings for Hetzner Cloud AArch64 instances
# https://wiki.nixos.org/wiki/Install_NixOS_on_Hetzner_Cloud#AArch64_(CAX_instance_type)_specifics
boot.initrd.kernelModules = [ "virtio_gpu" ];
boot.kernelParams = [ "console=tty" ];
# aarch64-linux
nixpkgs.hostPlatform = lib.mkDefault "aarch64-linux";
}

View File

@@ -0,0 +1,10 @@
{ config, lib, pkgs, modulesPath, ... }:
{
imports = [
(modulesPath + "/profiles/qemu-guest.nix")
./aarch64.nix
];
networking.useDHCP = lib.mkDefault true;
}