Merge pull request #266 from NixOS/interactive_vm

add interactive VM script
This commit is contained in:
lassulus 2025-06-15 15:32:52 +02:00 committed by GitHub
commit 0ececb1ddd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 122 additions and 16 deletions

View File

@ -77,6 +77,17 @@ $ nix develop .#linkcheck
$ ./checks/linkcheck/lychee.sh $ ./checks/linkcheck/lychee.sh
``` ```
## interactive VM
You can also run the wiki in an interactive vm by running
```nix
nix run .#interactive-vm
```
you can then access the wiki at localhost:4360 follow the output of the script
for more details (like passwords)
## FAQ: ## FAQ:
### When logging in with "GitHub auth", the app shows "Act on your behalf" as a permission. ### When logging in with "GitHub auth", the app shows "Act on your behalf" as a permission.

View File

@ -35,10 +35,16 @@
./targets/flake-module.nix ./targets/flake-module.nix
./modules/flake-module.nix ./modules/flake-module.nix
./checks/flake-module.nix ./checks/flake-module.nix
./vm/flake-module.nix
./formatter.nix ./formatter.nix
]; ];
perSystem = perSystem =
{ self', system, ... }: {
self',
system,
pkgs,
...
}:
{ {
checks = checks =

View File

@ -38,6 +38,11 @@ in
type = lib.types.str; type = lib.types.str;
description = "default Reply-To address in emails"; description = "default Reply-To address in emails";
}; };
testMode = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Enable test mode, which disables github login and uses a fixed admin password";
};
}; };
}; };
@ -49,7 +54,7 @@ in
database.type = "postgres"; database.type = "postgres";
nginx.hostName = config.services.nixos-wiki.hostname; nginx.hostName = config.services.nixos-wiki.hostname;
uploadsDir = "/var/lib/mediawiki-uploads/"; uploadsDir = "/var/lib/mediawiki-uploads/";
passwordFile = cfg.adminPasswordFile; passwordFile = if cfg.testMode then pkgs.writeText "pass" "nixos-wiki00" else cfg.adminPasswordFile;
extensions = { extensions = {
SyntaxHighlight_GeSHi = null; # provides <SyntaxHighlight> tags SyntaxHighlight_GeSHi = null; # provides <SyntaxHighlight> tags
@ -89,15 +94,17 @@ in
#$wgShowExceptionDetails = true; #$wgShowExceptionDetails = true;
# allow local login # allow local login
$wgAuthManagerOAuthConfig = [ ${lib.optionalString (!cfg.testMode) ''
'github' => [ $wgAuthManagerOAuthConfig = [
'clientId' => '${cfg.githubClientId}', 'github' => [
'clientSecret' => file_get_contents("${cfg.githubClientSecretFile}"), 'clientId' => '${cfg.githubClientId}',
'urlAuthorize' => 'https://github.com/login/oauth/authorize', 'clientSecret' => file_get_contents("${cfg.githubClientSecretFile}"),
'urlAccessToken' => 'https://github.com/login/oauth/access_token', 'urlAuthorize' => 'https://github.com/login/oauth/authorize',
'urlResourceOwnerDetails' => 'https://api.github.com/user' 'urlAccessToken' => 'https://github.com/login/oauth/access_token',
], 'urlResourceOwnerDetails' => 'https://api.github.com/user'
]; ],
];
''}
# Enable account creation globally # Enable account creation globally
$wgGroupPermissions['*']['createaccount'] = true; $wgGroupPermissions['*']['createaccount'] = true;
@ -150,9 +157,11 @@ in
$wgEmailConfirmToEdit = false; $wgEmailConfirmToEdit = false;
$wgAllowHTMLEmail = false; $wgAllowHTMLEmail = false;
$wgEmergencyContact = "${cfg.emergencyContact}"; ${lib.optionalString (!cfg.testMode) ''
$wgPasswordSender = "${cfg.passwordSender}"; $wgEmergencyContact = "${cfg.emergencyContact}";
$wgNoReplyAddress = "${cfg.noReplyAddress}"; $wgPasswordSender = "${cfg.passwordSender}";
$wgNoReplyAddress = "${cfg.noReplyAddress}";
''}
# To purge all page cache increase this using: date +%Y%m%d%H%M%S # To purge all page cache increase this using: date +%Y%m%d%H%M%S
$wgCacheEpoch = 20231115172319; $wgCacheEpoch = 20231115172319;
@ -244,8 +253,8 @@ in
limit_req_status 429; limit_req_status 429;
''; '';
services.nginx.virtualHosts.${config.services.mediawiki.nginx.hostName} = { services.nginx.virtualHosts.${config.services.mediawiki.nginx.hostName} = {
enableACME = lib.mkDefault true; enableACME = lib.mkDefault (!cfg.testMode);
forceSSL = lib.mkDefault true; forceSSL = lib.mkDefault (!cfg.testMode);
extraConfig = '' extraConfig = ''
limit_req zone=ip burst=20 nodelay; limit_req zone=ip burst=20 nodelay;
''; '';

80
vm/flake-module.nix Normal file
View File

@ -0,0 +1,80 @@
{ self, lib, ... }:
{
perSystem =
{ pkgs, ... }:
{
packages = lib.optionalAttrs pkgs.stdenv.isLinux {
interactive-vm = pkgs.writeShellApplication {
name = "interactive-vm";
runtimeInputs = [
];
text =
let
debugVm =
{ modulesPath, ... }:
{
imports = [
# The qemu-vm NixOS module gives us the `vm` attribute that we will later
# use, and other VM-related settings
"${modulesPath}/virtualisation/qemu-vm.nix"
];
# Forward the hosts's port 2222 to the guest's SSH port.
# Also, forward the other ports 1:1 from host to guest.
virtualisation.forwardPorts = [
{
from = "host";
host.port = 2222;
guest.port = 22;
}
{
from = "host";
host.port = 4360;
guest.port = 4360;
}
];
virtualisation.memorySize = 2048;
# Root user without password and enabled SSH for playing around
networking.firewall.enable = false;
services.openssh.enable = true;
services.openssh.permitRootLogin = "yes";
users.extraUsers.root.password = "nixos-wiki00"; # same as the admin user on the test wiki
environment.systemPackages = with pkgs; [
iptables
];
services.nginx.defaultListen = [
{
addr = "0.0.0.0";
port = 4360;
}
];
networking.firewall.allowedTCPPorts = [ 4360 ];
};
vmConfig = pkgs.nixos [
debugVm
self.nixosModules.nixos-wiki
{
security.acme.defaults.email = "example@example.com";
security.acme.defaults.server = "https://acme-staging-v02.api.letsencrypt.org/directory";
services.nixos-wiki = {
hostname = "localhost:4360";
testMode = true;
};
}
];
in
''
NIXOS_DISK_IMAGE=/tmp/wiki-vm.qcow2
export NIXOS_DISK_IMAGE
echo 'access the wiki after startup at http://localhost:4360'
echo 'user: admin, password: nixos-wiki00'
echo 'you can also SSH into the VM with: ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@localhost -p 2222'
echo 'password: nixos-wiki00'
${vmConfig.config.system.build.vm}/bin/run-nixos-vm
# TODO maybe clean up the qcow image?
'';
};
};
};
}