Skip to content

Commit 0ececb1

Browse files
authored
Merge pull request #266 from NixOS/interactive_vm
add interactive VM script
2 parents 8c6e9ac + 4e21cef commit 0ececb1

File tree

4 files changed

+122
-16
lines changed

4 files changed

+122
-16
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ $ nix develop .#linkcheck
7777
$ ./checks/linkcheck/lychee.sh
7878
```
7979

80+
## interactive VM
81+
82+
You can also run the wiki in an interactive vm by running
83+
84+
```nix
85+
nix run .#interactive-vm
86+
```
87+
88+
you can then access the wiki at localhost:4360 follow the output of the script
89+
for more details (like passwords)
90+
8091
## FAQ:
8192

8293
### When logging in with "GitHub auth", the app shows "Act on your behalf" as a permission.

flake.nix

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@
3535
./targets/flake-module.nix
3636
./modules/flake-module.nix
3737
./checks/flake-module.nix
38+
./vm/flake-module.nix
3839
./formatter.nix
3940
];
4041
perSystem =
41-
{ self', system, ... }:
42+
{
43+
self',
44+
system,
45+
pkgs,
46+
...
47+
}:
4248
{
4349

4450
checks =

modules/nixos-wiki/default.nix

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ in
3838
type = lib.types.str;
3939
description = "default Reply-To address in emails";
4040
};
41+
testMode = lib.mkOption {
42+
type = lib.types.bool;
43+
default = false;
44+
description = "Enable test mode, which disables github login and uses a fixed admin password";
45+
};
4146
};
4247
};
4348

@@ -49,7 +54,7 @@ in
4954
database.type = "postgres";
5055
nginx.hostName = config.services.nixos-wiki.hostname;
5156
uploadsDir = "/var/lib/mediawiki-uploads/";
52-
passwordFile = cfg.adminPasswordFile;
57+
passwordFile = if cfg.testMode then pkgs.writeText "pass" "nixos-wiki00" else cfg.adminPasswordFile;
5358

5459
extensions = {
5560
SyntaxHighlight_GeSHi = null; # provides <SyntaxHighlight> tags
@@ -89,15 +94,17 @@ in
8994
#$wgShowExceptionDetails = true;
9095
9196
# allow local login
92-
$wgAuthManagerOAuthConfig = [
93-
'github' => [
94-
'clientId' => '${cfg.githubClientId}',
95-
'clientSecret' => file_get_contents("${cfg.githubClientSecretFile}"),
96-
'urlAuthorize' => 'https://github.com/login/oauth/authorize',
97-
'urlAccessToken' => 'https://github.com/login/oauth/access_token',
98-
'urlResourceOwnerDetails' => 'https://api.github.com/user'
99-
],
100-
];
97+
${lib.optionalString (!cfg.testMode) ''
98+
$wgAuthManagerOAuthConfig = [
99+
'github' => [
100+
'clientId' => '${cfg.githubClientId}',
101+
'clientSecret' => file_get_contents("${cfg.githubClientSecretFile}"),
102+
'urlAuthorize' => 'https://github.com/login/oauth/authorize',
103+
'urlAccessToken' => 'https://github.com/login/oauth/access_token',
104+
'urlResourceOwnerDetails' => 'https://api.github.com/user'
105+
],
106+
];
107+
''}
101108
102109
# Enable account creation globally
103110
$wgGroupPermissions['*']['createaccount'] = true;
@@ -150,9 +157,11 @@ in
150157
$wgEmailConfirmToEdit = false;
151158
$wgAllowHTMLEmail = false;
152159
153-
$wgEmergencyContact = "${cfg.emergencyContact}";
154-
$wgPasswordSender = "${cfg.passwordSender}";
155-
$wgNoReplyAddress = "${cfg.noReplyAddress}";
160+
${lib.optionalString (!cfg.testMode) ''
161+
$wgEmergencyContact = "${cfg.emergencyContact}";
162+
$wgPasswordSender = "${cfg.passwordSender}";
163+
$wgNoReplyAddress = "${cfg.noReplyAddress}";
164+
''}
156165
157166
# To purge all page cache increase this using: date +%Y%m%d%H%M%S
158167
$wgCacheEpoch = 20231115172319;
@@ -244,8 +253,8 @@ in
244253
limit_req_status 429;
245254
'';
246255
services.nginx.virtualHosts.${config.services.mediawiki.nginx.hostName} = {
247-
enableACME = lib.mkDefault true;
248-
forceSSL = lib.mkDefault true;
256+
enableACME = lib.mkDefault (!cfg.testMode);
257+
forceSSL = lib.mkDefault (!cfg.testMode);
249258
extraConfig = ''
250259
limit_req zone=ip burst=20 nodelay;
251260
'';

vm/flake-module.nix

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{ self, lib, ... }:
2+
{
3+
perSystem =
4+
{ pkgs, ... }:
5+
{
6+
packages = lib.optionalAttrs pkgs.stdenv.isLinux {
7+
interactive-vm = pkgs.writeShellApplication {
8+
name = "interactive-vm";
9+
runtimeInputs = [
10+
];
11+
text =
12+
let
13+
debugVm =
14+
{ modulesPath, ... }:
15+
{
16+
imports = [
17+
# The qemu-vm NixOS module gives us the `vm` attribute that we will later
18+
# use, and other VM-related settings
19+
"${modulesPath}/virtualisation/qemu-vm.nix"
20+
];
21+
22+
# Forward the hosts's port 2222 to the guest's SSH port.
23+
# Also, forward the other ports 1:1 from host to guest.
24+
virtualisation.forwardPorts = [
25+
{
26+
from = "host";
27+
host.port = 2222;
28+
guest.port = 22;
29+
}
30+
{
31+
from = "host";
32+
host.port = 4360;
33+
guest.port = 4360;
34+
}
35+
];
36+
virtualisation.memorySize = 2048;
37+
38+
# Root user without password and enabled SSH for playing around
39+
networking.firewall.enable = false;
40+
services.openssh.enable = true;
41+
services.openssh.permitRootLogin = "yes";
42+
users.extraUsers.root.password = "nixos-wiki00"; # same as the admin user on the test wiki
43+
environment.systemPackages = with pkgs; [
44+
iptables
45+
];
46+
services.nginx.defaultListen = [
47+
{
48+
addr = "0.0.0.0";
49+
port = 4360;
50+
}
51+
];
52+
networking.firewall.allowedTCPPorts = [ 4360 ];
53+
};
54+
vmConfig = pkgs.nixos [
55+
debugVm
56+
self.nixosModules.nixos-wiki
57+
{
58+
security.acme.defaults.email = "[email protected]";
59+
security.acme.defaults.server = "https://acme-staging-v02.api.letsencrypt.org/directory";
60+
services.nixos-wiki = {
61+
hostname = "localhost:4360";
62+
testMode = true;
63+
};
64+
}
65+
];
66+
in
67+
''
68+
NIXOS_DISK_IMAGE=/tmp/wiki-vm.qcow2
69+
export NIXOS_DISK_IMAGE
70+
echo 'access the wiki after startup at http://localhost:4360'
71+
echo 'user: admin, password: nixos-wiki00'
72+
echo 'you can also SSH into the VM with: ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@localhost -p 2222'
73+
echo 'password: nixos-wiki00'
74+
${vmConfig.config.system.build.vm}/bin/run-nixos-vm
75+
# TODO maybe clean up the qcow image?
76+
'';
77+
};
78+
};
79+
};
80+
}

0 commit comments

Comments
 (0)