2018-11-26 06:51:00 -08:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
gce = pkgs.google-compute-engine;
|
|
|
|
in
|
2013-12-11 06:32:27 -08:00
|
|
|
{
|
2018-11-26 06:51:00 -08:00
|
|
|
imports = [
|
|
|
|
../profiles/headless.nix
|
|
|
|
../profiles/qemu-guest.nix
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
fileSystems."/" = {
|
2019-05-21 23:44:52 -07:00
|
|
|
fsType = "ext4";
|
2018-11-26 06:51:00 -08:00
|
|
|
device = "/dev/disk/by-label/nixos";
|
|
|
|
autoResize = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
boot.growPartition = true;
|
|
|
|
boot.kernelParams = [ "console=ttyS0" "panic=1" "boot.panic_on_fail" ];
|
|
|
|
boot.initrd.kernelModules = [ "virtio_scsi" ];
|
|
|
|
boot.kernelModules = [ "virtio_pci" "virtio_net" ];
|
|
|
|
|
2019-08-31 13:21:33 -07:00
|
|
|
# Generate a GRUB menu.
|
2018-11-26 06:51:00 -08:00
|
|
|
boot.loader.grub.device = "/dev/sda";
|
|
|
|
boot.loader.timeout = 0;
|
|
|
|
|
|
|
|
# Don't put old configurations in the GRUB menu. The user has no
|
|
|
|
# way to select them anyway.
|
|
|
|
boot.loader.grub.configurationLimit = 0;
|
|
|
|
|
2019-08-31 13:22:47 -07:00
|
|
|
# Allow root logins only using SSH keys
|
|
|
|
# and disable password authentication in general
|
2018-11-26 06:51:00 -08:00
|
|
|
services.openssh.enable = true;
|
|
|
|
services.openssh.permitRootLogin = "prohibit-password";
|
|
|
|
services.openssh.passwordAuthentication = mkDefault false;
|
|
|
|
|
2019-08-31 13:22:47 -07:00
|
|
|
# enable OS Login. This also requires setting enable-oslogin=TRUE metadata on
|
|
|
|
# instance or project level
|
|
|
|
security.googleOsLogin.enable = true;
|
|
|
|
|
2018-11-26 06:51:00 -08:00
|
|
|
# Use GCE udev rules for dynamic disk volumes
|
|
|
|
services.udev.packages = [ gce ];
|
|
|
|
|
|
|
|
# Force getting the hostname from Google Compute.
|
|
|
|
networking.hostName = mkDefault "";
|
|
|
|
|
|
|
|
# Always include cryptsetup so that NixOps can use it.
|
|
|
|
environment.systemPackages = [ pkgs.cryptsetup ];
|
|
|
|
|
|
|
|
# Make sure GCE image does not replace host key that NixOps sets
|
|
|
|
environment.etc."default/instance_configs.cfg".text = lib.mkDefault ''
|
|
|
|
[InstanceSetup]
|
|
|
|
set_host_keys = false
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Rely on GCP's firewall instead
|
|
|
|
networking.firewall.enable = mkDefault false;
|
|
|
|
|
|
|
|
# Configure default metadata hostnames
|
|
|
|
networking.extraHosts = ''
|
|
|
|
169.254.169.254 metadata.google.internal metadata
|
|
|
|
'';
|
|
|
|
|
|
|
|
networking.timeServers = [ "metadata.google.internal" ];
|
|
|
|
|
|
|
|
networking.usePredictableInterfaceNames = false;
|
|
|
|
|
|
|
|
# GC has 1460 MTU
|
|
|
|
networking.interfaces.eth0.mtu = 1460;
|
|
|
|
|
2021-01-13 02:01:01 -08:00
|
|
|
systemd.services.fetch-ssh-keys = {
|
|
|
|
description = "Fetch host keys and authorized_keys for root user";
|
|
|
|
|
|
|
|
wantedBy = [ "sshd.service" ];
|
|
|
|
before = [ "sshd.service" ];
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
|
|
|
|
script =
|
|
|
|
let
|
|
|
|
wget = "${pkgs.wget}/bin/wget --retry-connrefused -t 15 --waitretry=10 --header='Metadata-Flavor: Google'";
|
|
|
|
mktemp = "mktemp --tmpdir=/run";
|
|
|
|
in ''
|
|
|
|
# When dealing with cryptographic keys, we want to keep things private.
|
|
|
|
umask 077
|
|
|
|
mkdir -m 0700 -p /root/.ssh
|
|
|
|
|
|
|
|
echo "Obtaining SSH keys..."
|
|
|
|
AUTH_KEYS=$(${mktemp})
|
|
|
|
${wget} -O $AUTH_KEYS http://metadata.google.internal/computeMetadata/v1/instance/attributes/sshKeys
|
|
|
|
if [ -s $AUTH_KEYS ]; then
|
|
|
|
# Read in key one by one, split in case Google decided
|
|
|
|
# to append metadata (it does sometimes) and add to
|
|
|
|
# authorized_keys if not already present.
|
|
|
|
touch /root/.ssh/authorized_keys
|
|
|
|
NEW_KEYS=$(${mktemp})
|
|
|
|
# Yes this is a nix escape of two single quotes.
|
|
|
|
while IFS=''' read -r line || [[ -n "$line" ]]; do
|
|
|
|
keyLine=$(echo -n "$line" | cut -d ':' -f2)
|
|
|
|
IFS=' ' read -r -a array <<< "$keyLine"
|
|
|
|
if [ ''${#array[@]} -ge 3 ]; then
|
|
|
|
echo ''${array[@]:0:3} >> $NEW_KEYS
|
|
|
|
echo "Added ''${array[@]:2} to authorized_keys"
|
|
|
|
fi
|
|
|
|
done < $AUTH_KEYS
|
|
|
|
mv $NEW_KEYS /root/.ssh/authorized_keys
|
|
|
|
chmod 600 /root/.ssh/authorized_keys
|
|
|
|
rm -f $KEY_PUB
|
|
|
|
else
|
|
|
|
echo "Downloading http://metadata.google.internal/computeMetadata/v1/project/attributes/sshKeys failed."
|
|
|
|
false
|
|
|
|
fi
|
|
|
|
rm -f $AUTH_KEYS
|
|
|
|
|
|
|
|
SSH_HOST_KEYS_DIR=$(${mktemp} -d)
|
|
|
|
${wget} -O $SSH_HOST_KEYS_DIR/ssh_host_ed25519_key http://metadata.google.internal/computeMetadata/v1/instance/attributes/ssh_host_ed25519_key
|
|
|
|
${wget} -O $SSH_HOST_KEYS_DIR/ssh_host_ed25519_key.pub http://metadata.google.internal/computeMetadata/v1/instance/attributes/ssh_host_ed25519_key_pub
|
|
|
|
if [ -s $SSH_HOST_KEYS_DIR/ssh_host_ed25519_key -a -s $SSH_HOST_KEYS_DIR/ssh_host_ed25519_key.pub ]; then
|
|
|
|
mv -f $SSH_HOST_KEYS_DIR/ssh_host_ed25519_key* /etc/ssh/
|
|
|
|
chmod 600 /etc/ssh/ssh_host_ed25519_key
|
|
|
|
chmod 644 /etc/ssh/ssh_host_ed25519_key.pub
|
|
|
|
else
|
|
|
|
echo "Setup of ssh host keys from http://metadata.google.internal/computeMetadata/v1/instance/attributes/ failed."
|
|
|
|
false
|
|
|
|
fi
|
|
|
|
rm -rf $SSH_HOST_KEYS_DIR
|
|
|
|
'';
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
serviceConfig.StandardError = "journal+console";
|
|
|
|
serviceConfig.StandardOutput = "journal+console";
|
|
|
|
};
|
|
|
|
|
2018-11-26 06:51:00 -08:00
|
|
|
systemd.services.google-instance-setup = {
|
|
|
|
description = "Google Compute Engine Instance Setup";
|
2019-08-31 13:42:34 -07:00
|
|
|
after = [ "network-online.target" "network.target" "rsyslog.service" ];
|
|
|
|
before = [ "sshd.service" ];
|
2019-08-31 15:28:28 -07:00
|
|
|
path = with pkgs; [ coreutils ethtool openssh ];
|
2018-11-26 06:51:00 -08:00
|
|
|
serviceConfig = {
|
2019-08-31 13:42:34 -07:00
|
|
|
ExecStart = "${gce}/bin/google_instance_setup";
|
|
|
|
StandardOutput="journal+console";
|
2018-11-26 06:51:00 -08:00
|
|
|
Type = "oneshot";
|
|
|
|
};
|
2019-08-31 13:42:34 -07:00
|
|
|
wantedBy = [ "sshd.service" "multi-user.target" ];
|
2018-11-26 06:51:00 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.google-network-daemon = {
|
|
|
|
description = "Google Compute Engine Network Daemon";
|
2019-08-31 13:42:34 -07:00
|
|
|
after = [ "network-online.target" "network.target" "google-instance-setup.service" ];
|
2018-11-26 06:51:00 -08:00
|
|
|
path = with pkgs; [ iproute ];
|
|
|
|
serviceConfig = {
|
2019-08-31 13:42:34 -07:00
|
|
|
ExecStart = "${gce}/bin/google_network_daemon";
|
|
|
|
StandardOutput="journal+console";
|
|
|
|
Type="simple";
|
2018-11-26 06:51:00 -08:00
|
|
|
};
|
2019-08-31 13:42:34 -07:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2018-11-26 06:51:00 -08:00
|
|
|
};
|
|
|
|
|
2019-08-31 13:42:34 -07:00
|
|
|
systemd.services.google-clock-skew-daemon = {
|
|
|
|
description = "Google Compute Engine Clock Skew Daemon";
|
|
|
|
after = [ "network.target" "google-instance-setup.service" "google-network-daemon.service" ];
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${gce}/bin/google_clock_skew_daemon";
|
|
|
|
StandardOutput="journal+console";
|
|
|
|
Type = "simple";
|
|
|
|
};
|
|
|
|
wantedBy = ["multi-user.target"];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-11-26 06:51:00 -08:00
|
|
|
systemd.services.google-shutdown-scripts = {
|
|
|
|
description = "Google Compute Engine Shutdown Scripts";
|
|
|
|
after = [
|
|
|
|
"network-online.target"
|
|
|
|
"network.target"
|
|
|
|
"rsyslog.service"
|
|
|
|
"google-instance-setup.service"
|
|
|
|
"google-network-daemon.service"
|
|
|
|
];
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${pkgs.coreutils}/bin/true";
|
2019-08-31 13:42:34 -07:00
|
|
|
ExecStop = "${gce}/bin/google_metadata_script_runner --script-type shutdown";
|
2018-11-26 06:51:00 -08:00
|
|
|
RemainAfterExit = true;
|
2019-08-31 13:42:34 -07:00
|
|
|
StandardOutput="journal+console";
|
|
|
|
TimeoutStopSec = "0";
|
|
|
|
Type = "oneshot";
|
2018-11-26 06:51:00 -08:00
|
|
|
};
|
2019-08-31 13:42:34 -07:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2018-11-26 06:51:00 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.google-startup-scripts = {
|
|
|
|
description = "Google Compute Engine Startup Scripts";
|
|
|
|
after = [
|
|
|
|
"network-online.target"
|
|
|
|
"network.target"
|
|
|
|
"rsyslog.service"
|
|
|
|
"google-instance-setup.service"
|
|
|
|
"google-network-daemon.service"
|
|
|
|
];
|
|
|
|
serviceConfig = {
|
2019-08-31 13:42:34 -07:00
|
|
|
ExecStart = "${gce}/bin/google_metadata_script_runner --script-type startup";
|
2018-11-26 06:51:00 -08:00
|
|
|
KillMode = "process";
|
2019-08-31 13:42:34 -07:00
|
|
|
StandardOutput = "journal+console";
|
2018-11-26 06:51:00 -08:00
|
|
|
Type = "oneshot";
|
|
|
|
};
|
2019-08-31 13:42:34 -07:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2018-11-26 06:51:00 -08:00
|
|
|
};
|
|
|
|
|
2019-08-31 17:55:28 -07:00
|
|
|
environment.etc."sysctl.d/11-gce-network-security.conf".source = "${gce}/sysctl.d/11-gce-network-security.conf";
|
2013-12-11 06:32:27 -08:00
|
|
|
}
|