From eca08c74169d1dd20c13bfa3500145c09c8507a8 Mon Sep 17 00:00:00 2001 From: Vladimir Timofeenko Date: Fri, 23 Dec 2022 18:57:34 -0800 Subject: [PATCH] nix: add development vm This commit adds back the ability to run a development VM with docspell. See updated documentation for examples. --- nix/checks/configuration-test.nix | 7 ++- nix/dev-vm/default.nix | 28 ++++++++++++ nix/flake.nix | 15 +++++++ website/site/content/docs/dev/development.md | 46 ++++++++------------ 4 files changed, 67 insertions(+), 29 deletions(-) create mode 100644 nix/dev-vm/default.nix diff --git a/nix/checks/configuration-test.nix b/nix/checks/configuration-test.nix index c78f9b33..bdd3000a 100644 --- a/nix/checks/configuration-test.nix +++ b/nix/checks/configuration-test.nix @@ -15,7 +15,7 @@ in i18n = { defaultLocale = "en_US.UTF-8"; }; - console.keyMap = "de"; + console.keyMap = "us"; users.users.root = { password = "root"; @@ -88,6 +88,9 @@ in firewall.allowedTCPPorts = [ 7880 ]; }; - system.stateVersion = "22.05"; + system.stateVersion = "22.11"; + + # This slows down the build of a vm + documentation.enable = false; } diff --git a/nix/dev-vm/default.nix b/nix/dev-vm/default.nix new file mode 100644 index 00000000..6f4e93f6 --- /dev/null +++ b/nix/dev-vm/default.nix @@ -0,0 +1,28 @@ +# NOTE: modulesPath and imports are taken from nixpkgs#59219 +{ modulesPath, pkgs, lib, ... }: { + imports = [ (modulesPath + "/virtualisation/qemu-vm.nix") ]; + services.openssh = { + enable = true; + permitRootLogin = "yes"; + }; + + services.docspell-restserver = { + openid = lib.mkForce [ ]; + backend = lib.mkForce { + signup = { + mode = "open"; + }; + }; + }; + + # Otherwise oomkiller kills docspell + virtualisation.memorySize = 2048; + + virtualisation.forwardPorts = [ + # SSH + { from = "host"; host.port = 64022; guest.port = 22; } + # Docspell + { from = "host"; host.port = 64080; guest.port = 7880; } + ]; + +} diff --git a/nix/flake.nix b/nix/flake.nix index 633f3cd3..38a87989 100644 --- a/nix/flake.nix +++ b/nix/flake.nix @@ -115,5 +115,20 @@ joex = ((import ./modules/joex.nix) self.overlays.default); }; + nixosConfigurations = + let + lib = nixpkgs.lib; + in + { + dev-vm = lib.makeOverridable nixpkgs.lib.nixosSystem { + system = "x86_64-linux"; + modules = [ + self.nixosModules.default + ./checks + # nixos-shell specific module. Should be kept outside nix flake checks + ./dev-vm + ]; + }; + }; }; } diff --git a/website/site/content/docs/dev/development.md b/website/site/content/docs/dev/development.md index 3720403c..3cc95543 100644 --- a/website/site/content/docs/dev/development.md +++ b/website/site/content/docs/dev/development.md @@ -126,43 +126,35 @@ warnings should also be fixed. # Nix Expressions -The directory `/nix` contains nix expressions to install docspell via +The directory `/nix` contains Nix Flake to install docspell via the nix package manager and to integrate it into NixOS. -## Testing NixOS Modules +Flake implements `checks` output which can be run with `nix flake check` +and it defines a development VM which can be used to interactively work +with docspell. -The modules can be build by building the `configuration-test.nix` file -together with some nixpkgs version. For example: +To run the VM, issue: -``` bash -nixos-rebuild build-vm -I nixos-config=./configuration-test.nix \ - -I nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/nixos-19.09.tar.gz +```bash +cd $PROJECT_ROOT/nix +nix run '.#nixosConfigurations.dev-vm.config.system.build.vm ``` -This will build all modules imported in `configuration-test.nix` and -create a virtual machine containing the system. After that completes, -the system configuration can be found behind the `./result/system` -symlink. So it is possible to look at the generated systemd config for -example: +To open docspell, wait for docspell-restserver service to report that +http listener is up and connect to `localhost:64080`. -``` bash -cat result/system/etc/systemd/system/docspell-joex.service +To ssh into the machine, run: + +```bash +ssh -o StrictHostKeyChecking=no \ + -o UserKnownHostsFile=/dev/null \ + -p 64022 root@localhost ``` -And with some more commands (there probably is an easier way…) the -config file can be checked: +Once connected to the machine, you can see the docspell config file via -``` bash -cat result/system/etc/systemd/system/docspell-joex.service | grep ExecStart | cut -d'=' -f2 | xargs cat | tail -n1 | awk '{print $NF}'| sed 's/.$//' | xargs cat | jq -``` - -To see the module in action, the vm can be started (the first line -sets more memory for the vm): - -``` bash -export QEMU_OPTS="-m 2048" -export QEMU_NET_OPTS "hostfwd=tcp::7880-:7880" -./result/bin/run-docspelltest-vm +```bash +systemd-show docspell-joex.service | grep ExecStart | cut -d'=' -f2 | xargs cat | tail -n1 | awk '{print $NF}'| sed 's/.$//' | xargs cat | jq ``` # Release