self-host-playbook/configuration.nix

96 lines
2.3 KiB
Nix
Raw Normal View History

2025-02-18 08:50:17 +01:00
{
2025-03-28 10:13:26 +01:00
config,
2025-02-18 08:50:17 +01:00
lib,
pkgs,
2025-03-28 10:13:26 +01:00
self,
2025-02-18 08:50:17 +01:00
...
}:
# Read configuration from JSON
let
jsonConfig = builtins.fromJSON (builtins.readFile ./config.json);
2025-03-28 10:13:26 +01:00
customServicesDir = ./custom-services;
customServicesExists = builtins.pathExists customServicesDir;
2025-02-18 08:50:17 +01:00
2025-03-28 10:13:26 +01:00
customServices =
if customServicesExists
then
map
(name: ./custom-services + "/${name}")
(builtins.filter
(name: lib.hasSuffix ".nix" name)
(builtins.attrNames (builtins.readDir customServicesDir)))
else [];
in {
imports =
[
./disko-config.nix
]
++ customServices;
2025-02-18 08:50:17 +01:00
2025-03-28 10:13:26 +01:00
options.nixosConfig.flake = lib.mkOption {
type = lib.types.path;
description = "Path to the current flake configuration";
2025-02-18 08:50:17 +01:00
};
2025-03-28 10:13:26 +01:00
config = {
nix.settings = {
trusted-users = [jsonConfig.username];
2025-02-18 08:50:17 +01:00
};
2025-03-28 10:13:26 +01:00
# Set the flake path
nixosConfig.flake = self;
2025-02-18 08:50:17 +01:00
2025-03-28 10:13:26 +01:00
# Activation script to save the configuration
system.activationScripts.saveFlakeConfig = {
deps = [];
text = ''
rm -rf /etc/nixos/current-systemconfig
mkdir -p /etc/nixos/current-systemconfig
cp -rf ${config.nixosConfig.flake}/* /etc/nixos/current-systemconfig/
cd /etc/nixos/current-systemconfig
chown -R ${jsonConfig.username}:users /etc/nixos/current-systemconfig
chmod -R u=rwX,g=rX,o=rX /etc/nixos/current-systemconfig
'';
2025-02-18 08:50:17 +01:00
};
2025-03-28 10:13:26 +01:00
services.selfHostPlaybook = {
2025-02-18 08:50:17 +01:00
enable = true;
2025-03-28 10:13:26 +01:00
tier = "starter"; # This determines which services are enabled
2025-02-18 08:50:17 +01:00
};
2025-03-28 10:13:26 +01:00
# Networking
networking = {
2025-04-11 09:36:53 +02:00
hostName = jsonConfig.hostname;
2025-03-28 10:13:26 +01:00
firewall = {
enable = true;
# Only allow necessary ports
allowedTCPPorts = [80 443 2222]; # HTTP, HTTPS, and SSH
2025-02-18 08:50:17 +01:00
};
2025-03-28 10:13:26 +01:00
};
2025-02-18 08:50:17 +01:00
2025-03-28 10:13:26 +01:00
environment.etc = {
environment-files = {
source = ./env;
2025-02-18 08:50:17 +01:00
};
};
2025-03-28 10:13:26 +01:00
# User configuration
users.users.${jsonConfig.username} = {
isNormalUser = true;
extraGroups = ["wheel" "docker"];
hashedPassword = jsonConfig.hashedPassword;
openssh.authorizedKeys.keys = [jsonConfig.sshKey];
# Set default shell to bash
shell = pkgs.bash;
2025-02-18 09:53:28 +01:00
};
2025-03-28 10:13:26 +01:00
programs.git = {
enable = true;
config = {
user.name = jsonConfig.username;
user.email = "${jsonConfig.username}@nixos";
safe.directory = "/etc/nixos/current-systemconfig";
};
};
2025-02-18 09:53:28 +01:00
};
2025-02-18 08:50:17 +01:00
}