depot/ops/nixos/lib/common.nix

227 lines
7 KiB
Nix
Raw Normal View History

# SPDX-FileCopyrightText: 2020 Luke Granger-Brown <depot@lukegb.com>
#
# SPDX-License-Identifier: Apache-2.0
2020-10-25 11:36:16 +00:00
{ pkgs, config, depot, lib, rebuilder, ... }@args:
let
inherit (lib) mkDefault;
2021-03-20 12:39:23 +00:00
switch-prebuilt = import ./switch-prebuilt.nix args;
in
{
imports = [ ../../../third_party/home-manager/nixos ];
2021-04-10 20:15:30 +00:00
options.my.rundeck.hostname = lib.mkOption {
type = lib.types.str;
default = config.networking.fqdn;
};
options.my.rundeck.expectedOnline = lib.mkOption {
type = lib.types.bool;
default = true;
};
options.my.rundeck.tags = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ "nixos" ];
};
2020-10-25 11:36:16 +00:00
options.my.home-manager.imports = lib.mkOption {
type = lib.types.listOf lib.types.path;
default = [ ./home-manager/common.nix ];
};
options.my.home-manager.system = lib.mkOption {
type = lib.types.nullOr lib.types.anything;
default = null;
2020-05-08 23:03:21 +00:00
};
options.my.prometheus.additionalExporterPorts = lib.mkOption {
type = lib.types.attrsOf lib.types.port;
default = {};
};
options.my.ip.tailscale = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
};
options.my.deploy.enable = lib.mkOption {
type = lib.types.bool;
default = true;
};
options.my.deploy.args = lib.mkOption {
type = lib.types.str;
default = "";
};
2020-10-25 11:36:16 +00:00
config = {
hardware.enableRedistributableFirmware = true;
networking.search = [
2021-03-20 15:01:28 +00:00
"int.as205479.net"
"as205479.net"
];
my.rundeck.tags = [ "nixos" ];
2020-10-25 11:36:16 +00:00
nix = {
nixPath = [ "depot=/home/lukegb/depot/" "nixpkgs=/home/lukegb/depot/third_party/nixpkgs/" ];
trustedUsers = [ "root" "@wheel" "deployer" ];
2020-10-25 11:36:16 +00:00
binaryCaches = lib.mkForce [ "https://cache.nixos.org/" "s3://lukegb-nix-cache?endpoint=storage.googleapis.com&trusted=1" ];
trustedBinaryCaches = lib.mkForce [ "https://cache.nixos.org/" "s3://lukegb-nix-cache?endpoint=storage.googleapis.com&trusted=1" ];
envVars = {
AWS_ACCESS_KEY_ID = "${depot.ops.secrets.nixCache.AWS_ACCESS_KEY_ID}";
AWS_SECRET_ACCESS_KEY = "${depot.ops.secrets.nixCache.AWS_SECRET_ACCESS_KEY}";
};
};
nixpkgs.config = depot.third_party.nixpkgsConfig;
2020-10-25 11:36:16 +00:00
i18n.defaultLocale = "en_GB.UTF-8";
console.keyMap = "us";
2020-10-25 11:36:16 +00:00
time.timeZone = mkDefault "Etc/UTC";
2020-11-05 02:03:20 +00:00
zramSwap = {
enable = true;
memoryMax = 4 * 1024 * 1024 * 1024;
};
2020-10-25 11:36:16 +00:00
environment.systemPackages = with pkgs; [
2020-11-17 02:21:46 +00:00
vim rxvt_unicode.terminfo tmux rebuilder tailscale rsync libarchive tcpdump restic
2021-04-17 19:17:43 +00:00
alacritty.terminfo
2021-03-20 12:11:45 +00:00
iftop htop jq
2021-04-10 22:17:28 +00:00
depot.nix.pkgs.mercurial
2021-03-20 12:39:23 +00:00
switch-prebuilt
2020-10-25 11:36:16 +00:00
];
2020-10-25 11:36:16 +00:00
networking.firewall = {
allowPing = true;
};
2020-10-25 11:36:16 +00:00
2020-10-25 12:00:15 +00:00
environment.homeBinInPath = true;
security.pam.enableSSHAgentAuth = true;
2020-10-25 12:00:15 +00:00
2020-10-25 11:36:16 +00:00
users.mutableUsers = false;
users.users = let secrets = depot.ops.secrets; in {
root.hashedPassword = secrets.passwordHashes.root;
lukegb = {
isNormalUser = true;
uid = 1000;
extraGroups = [ "wheel" "audio" ];
2020-10-25 11:36:16 +00:00
hashedPassword = secrets.passwordHashes.lukegb;
openssh.authorizedKeys.keyFiles = [
../../secrets/lukegb_totoro.pub
../../secrets/lukegb_termius.pub
../../secrets/lukegb_porcorosso_win.pub
../../secrets/lukegb_porcorosso_wsl.pub
2020-11-03 15:25:03 +00:00
../../secrets/lukegb_porcorosso_linux.pub
../../secrets/lukegb_red_solo.pub
];
2020-10-25 11:36:16 +00:00
};
deployer = {
isSystemUser = true;
uid = 1001;
hashedPassword = "!";
useDefaultShell = true;
home = "/var/lib/deployer";
createHome = true;
openssh.authorizedKeys.keyFiles = [
../../secrets/deployer_ed25519.pub
../../secrets/rundeck_deployer_rsa.pub
2020-10-25 11:36:16 +00:00
];
};
2020-05-09 00:10:20 +00:00
};
2020-10-25 11:36:16 +00:00
security.sudo.extraRules = [{
users = [ "deployer" ];
commands = [{
command = "${rebuilder}/bin/rebuilder";
options = [ "NOPASSWD" ];
2021-03-20 12:39:23 +00:00
} {
command = "${switch-prebuilt}/bin/switch-prebuilt";
options = [ "NOPASSWD" ];
2020-10-25 11:36:16 +00:00
}];
2020-05-09 00:10:20 +00:00
}];
2020-10-25 11:36:16 +00:00
security.sudo.extraConfig = ''
Defaults:deployer !requiretty
'';
2020-10-25 11:36:16 +00:00
programs.mtr.enable = true;
services.openssh.enable = true;
services.tailscale.enable = true;
networking.firewall.interfaces.tailscale0 = {
# Just allow anything in on tailscale0.
allowedTCPPortRanges = [{ from = 0; to = 65535; }];
allowedUDPPortRanges = [{ from = 0; to = 65535; }];
};
2020-10-25 11:36:16 +00:00
boot = {
kernelModules = [ "tcp_bbr" ];
kernel.sysctl."net.ipv4.tcp_congestion_control" = "bbr";
kernel.sysctl."net.core.default_qdisc" = "fq_codel";
};
2020-10-25 11:36:16 +00:00
# Clean up daily.
nix.gc = {
automatic = lib.mkDefault true;
dates = "*-*-* 05:00:00";
2021-03-19 21:29:54 +00:00
options = "--delete-older-than 7d";
2020-10-25 11:36:16 +00:00
};
home-manager.useUserPackages = true;
home-manager.useGlobalPkgs = true;
systemd.services."home-manager-lukegb" = {
before = [ "display-manager.service" ];
wantedBy = [ "multi-user.target" ];
};
2020-10-25 11:36:16 +00:00
home-manager.users.lukegb = { pkgs, ... }: ({
imports = [ ({
_module.args = args // {
configName = null;
};
2020-10-25 11:36:16 +00:00
})] ++ config.my.home-manager.imports ++ (
lib.optional (config.my.home-manager.system != null) config.my.home-manager.system
);
});
services.prometheus.exporters.node = {
enable = true;
enabledCollectors = [ "systemd" "textfile" ];
extraFlags = [
"--collector.textfile.directory=/run/prometheus-textfile-exports"
];
};
system.activationScripts.node-exporter = {
text = ''
test -d /run/prometheus-textfile-exports || mkdir /run/prometheus-textfile-exports
my_version_string="$(cat "$systemConfig/nixos-version")"
my_hash_string="$(readlink -f "$systemConfig" | grep -Eo '\b[0-9a-df-np-sv-z]{32}\b')"
echo "nixos_running_system{version=\"$my_version_string\", hash=\"$my_hash_string\"} 1" > /run/prometheus-textfile-exports/running_system.prom
my_version_string="$(cat "/run/booted-system/nixos-version")"
my_hash_string="$(readlink -f "/run/booted-system" | grep -Eo '\b[0-9a-df-np-sv-z]{32}\b')"
echo "nixos_booted_system{version=\"$my_version_string\", hash=\"$my_hash_string\"} 1" > /run/prometheus-textfile-exports/booted_system.prom
'';
};
boot.postBootCommands = lib.mkAfter ''
test -d /run/prometheus-textfile-exports || mkdir /run/prometheus-textfile-exports
my_version_string="$(cat "/run/booted-system/nixos-version")"
my_hash_string="$(readlink -f "/run/booted-system" | grep -Eo '\b[0-9a-df-np-sv-z]{32}\b')"
echo "nixos_booted_system{version=\"$my_version_string\", hash=\"$my_hash_string\"} 1" > /run/prometheus-textfile-exports/booted_system.prom
'';
system.nixos.tags = lib.mkBefore [
depot.version
];
2021-01-19 23:41:47 +00:00
services.nginx = {
recommendedTlsSettings = true;
recommendedOptimisation = true;
recommendedGzipSettings = true;
recommendedProxySettings = true;
};
2021-01-30 18:47:12 +00:00
services.fwupd.enable = true;
networking.hosts."2a02:26f0:5700:3b1::2ab3" = [ "www.nhs.uk" ];
};
2020-07-08 18:36:21 +00:00
}