From 709441590bfe576ebba8843e2842b803bc0ff8dc Mon Sep 17 00:00:00 2001 From: Markus Schmidl Date: Wed, 22 Nov 2023 23:53:42 +0100 Subject: [PATCH] add test-vm --- .gitignore | 1 + flake.nix | 50 ++++++++++++++++++++++++++++++++++++++-- tests/vm/default.nix | 55 ++++++++++++++++++++++++++++++++++++++++++++ tests/vm/lizard.nix | 29 +++++++++++++++++++++++ 4 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 tests/vm/default.nix create mode 100644 tests/vm/lizard.nix diff --git a/.gitignore b/.gitignore index 1fbc882..a82d537 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target result .direnv +nixos.qcow2 diff --git a/flake.nix b/flake.nix index f0cfa04..355914b 100644 --- a/flake.nix +++ b/flake.nix @@ -33,11 +33,33 @@ rustc = toolchain; }).buildPackage; }; + + test-vm-pkg = self.nixosConfigurations.lizard-mctest.config.system.build.vm; in rec { checks = packages; packages = { lizard = package; + test-vm = test-vm-pkg; + test-vm-wrapper = pkgs.writeScript "trekkie-test-vm-wrapper" + '' + set -e + + echo Trekkie-McTest: enterprise-grade, free-range, grass fed testing vm + echo + echo "ALL RELEVANT SERVICES WILL BE EXPOSED TO THE HOST:" + echo -e "Service\t\tPort" + echo -e "SSH:\t\t2222\troot:lol" + echo -e "lizard:\t8060" + echo -e "redis:\t\t8061" + echo + + set -x + export QEMU_NET_OPTS="hostfwd=tcp::2222-:22,hostfwd=tcp::8060-:8060,hostfwd=tcp::8061-:6379" + + echo "running the vm now..." + ${self.packages.${system}.test-vm}/bin/run-nixos-vm + ''; default = package; docs = (pkgs.nixosOptionsDoc { options = (nixpkgs.lib.nixosSystem { @@ -47,6 +69,14 @@ }).optionsCommonMark; }; + # to get yourself a virtualized testing playground: + # nix run .\#mctest + apps = { + mctest = { + type = "app"; + program = "${self.packages.${system}.test-vm-wrapper}"; + }; + }; devShells.default = pkgs.mkShell { nativeBuildInputs = (with packages.lizard; nativeBuildInputs ++ buildInputs); }; @@ -57,12 +87,28 @@ }; }) // { nixosModules = rec { - default = funnel; - funnel = import ./nixos-module; + default = lizard; + lizard = import ./nixos-module; }; overlays.default = final: prev: { inherit (self.packages.${prev.system}) lizard; }; + + # qemu vm for testing + nixosConfigurations.lizard-mctest = nixpkgs.lib.nixosSystem { + system = "x86_64-linux"; + specialArgs = { inherit inputs; }; + modules = [ + self.nixosModules.default + ./tests/vm + + { + nixpkgs.overlays = [ + self.overlays.default + ]; + } + ]; + }; }; } diff --git a/tests/vm/default.nix b/tests/vm/default.nix new file mode 100644 index 0000000..4e46422 --- /dev/null +++ b/tests/vm/default.nix @@ -0,0 +1,55 @@ +# add-hoc config for a test VM. +{ inputs, modulesPath, ... }: +{ + imports = [ + "${modulesPath}/virtualisation/qemu-vm.nix" + ./lizard.nix + ]; + + fileSystems = { + "/" = { + device = "/dev/disk/by-label/nixos"; + fsType = "ext4"; + }; + }; + + boot = { + kernelParams = [ "console=ttyS0" "boot.shell_on_fail" ]; + loader.timeout = 5; + }; + + virtualisation = { + diskSize = 512; + memorySize = 512; + graphics = false; + }; + + services.getty = { + autologinUser = "root"; + }; + users.motd = '' + Trekkie-McTest: enterprise-grade, free-range, grass-fed testing vm + Now with 100% less graphics! + + Services exposed to the host: + lizard: 8060 + SSH: 2222 + redis: 8061 + + root password is "lol" + + have fun! + ''; + + + networking.firewall.enable = false; + + users.mutableUsers = false; + users.users.root.password = "lol"; + services.openssh = { + enable = true; + permitRootLogin = "yes"; + }; + + system.stateVersion = "22.11"; +} diff --git a/tests/vm/lizard.nix b/tests/vm/lizard.nix new file mode 100644 index 0000000..ad47ab4 --- /dev/null +++ b/tests/vm/lizard.nix @@ -0,0 +1,29 @@ +{ config, inputs, ... }: +{ + TLMS.lizard = { + # Unlike the production, we do not reverse-proxy the lizard, we just expose + # port directly to the host vm. + enable = true; + http = { + host = "0.0.0.0"; + port = 8060; + }; + redis = { + port = 6379; + host = "localhost"; + }; + logLevel = "info"; + }; + systemd.services."lizard" = { + after = [ "redis-lizard.service" ]; + wants = [ "redis-lizard.service" ]; + }; + + services = { + redis.servers."lizard" = { + enable = true; + bind = config.TLMS.lizard.redis.host; + port = config.TLMS.lizard.redis.port; + }; + }; +}