diff --git a/factorio/Makefile b/factorio/Makefile new file mode 100644 index 0000000..dd69314 --- /dev/null +++ b/factorio/Makefile @@ -0,0 +1,8 @@ +.PHONY: deploy +deploy: + nixos-rebuild switch --fast -I nixos-config=./configuration.nix --build-host root@factorio.chandlerswift.com --target-host root@factorio.chandlerswift.com + +.PHONY: create-server +create-server: + ../create-server.py factorio.chandlerswift.com + diff --git a/factorio/README.md b/factorio/README.md new file mode 100644 index 0000000..9ea20b7 --- /dev/null +++ b/factorio/README.md @@ -0,0 +1 @@ +# factorio.chandlerswift.com diff --git a/factorio/configuration.nix b/factorio/configuration.nix new file mode 100644 index 0000000..9f31190 --- /dev/null +++ b/factorio/configuration.nix @@ -0,0 +1,61 @@ +{ config, lib, pkgs, ... }: { + + nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ + "factorio-headless" + ]; + + imports = [ + ./hardware-configuration.nix + ]; + + networking.hostName = "factorio"; + time.timeZone = "America/Chicago"; + i18n.defaultLocale = "en_US.UTF-8"; + + environment.systemPackages = with pkgs; [ rsync ]; + + services.openssh.enable = true; + users.users.root.openssh.authorizedKeys.keys = [''ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEhPyyqS3BGYor3zLbjc8hZuhem3mS8TNmvWogXcnz/b chandler@chandlerswift.com'' ]; + + services.factorio = { + enable = true; + openFirewall = true; + nonBlockingSaving = true; + game-name = "Chandler's Factorio Server"; + game-password = "nixosftw"; + description = "Hi Jeff!"; + mods = + let + inherit (pkgs) lib; + modDir = ./mods; + modList = lib.pipe modDir [ + builtins.readDir + (lib.filterAttrs (k: v: v == "regular")) + (lib.mapAttrsToList (k: v: k)) + (builtins.filter (lib.hasSuffix ".zip")) + ]; + modToDrv = modFileName: + pkgs.runCommand "copy-factorio-mods" {} '' + mkdir $out + cp ${modDir + "/${modFileName}"} $out/${modFileName} + '' + // { deps = []; }; + in + builtins.map modToDrv modList; + }; + +# networking.firewall.allowedTCPPorts = [ +# 80 # Caddy +# 443 # Caddy +# ]; + # networking.firewall.allowedUDPPorts = [ ... ]; + + # This option defines the first version of NixOS you have installed on this particular machine, + # and is used to maintain compatibility with application data (e.g. databases) created on older NixOS versions. + # + # Most users should NEVER change this value after the initial install, for any reason, + # even if you've upgraded your system to a new NixOS release. + system.stateVersion = "24.05"; + +} + diff --git a/factorio/hardware-configuration.nix b/factorio/hardware-configuration.nix new file mode 100644 index 0000000..9bf6dc6 --- /dev/null +++ b/factorio/hardware-configuration.nix @@ -0,0 +1,19 @@ +{ modulesPath, ... }: +{ + imports = [ (modulesPath + "/profiles/qemu-guest.nix") ]; + boot.loader.grub = { + efiSupport = true; + efiInstallAsRemovable = true; + device = "nodev"; + }; + fileSystems."/boot" = { device = "/dev/disk/by-uuid/FDD2-F69B"; fsType = "vfat"; }; + boot.initrd.availableKernelModules = [ "ata_piix" "uhci_hcd" "xen_blkfront" "vmw_pvscsi" ]; + boot.initrd.kernelModules = [ "nvme" ]; + fileSystems."/" = { device = "/dev/vda2"; fsType = "ext4"; }; + swapDevices = [{ + device = "/swapfile"; + size = 4 * 1024; # 4GB + }]; + boot.tmp.cleanOnBoot = true; + zramSwap.enable = true; +} diff --git a/factorio/install_mods.sh b/factorio/install_mods.sh new file mode 100755 index 0000000..39c547b --- /dev/null +++ b/factorio/install_mods.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env nix-shell +#! nix-shell -i bash -p bash curl jq +mods="Krastorio2 Krastorio2Assets flib Big_Brother AtomicArtillery AutoDeconstruct nixie-tubes" +download_dir=./mods +username=$(cat ../keys.toml | grep factorio-username | cut -d'"' -f2) +token=$(cat ../keys.toml | grep factorio-token | cut -d'"' -f2) +for mod in $mods; do + mod_data=$(curl -s https://mods.factorio.com/api/mods/$mod) + download_url=$(<<<$mod_data jq -r ".releases[-1].download_url") + file_name=$(<<<$mod_data jq -r ".releases[-1].file_name") + sha1=$(<<<$mod_data jq -r ".releases[-1].sha1") # TODO + [ -f $download_dir/$file_name ] || curl --location --no-clobber --output $download_dir/$file_name "https://mods.factorio.com/$download_url?username=$username&token=$token" +done diff --git a/factorio/mods/.gitignore b/factorio/mods/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/factorio/mods/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore