|
| 1 | +{ |
| 2 | + config, |
| 3 | + lib, |
| 4 | + pkgs, |
| 5 | + ... |
| 6 | +}: |
| 7 | + |
| 8 | +with lib; |
| 9 | + |
| 10 | +let |
| 11 | + cfg = config.services.nixos-wiki.pages; |
| 12 | + |
| 13 | + # Path to pages directory |
| 14 | + pagesDir = lib.fileset.toSource { |
| 15 | + root = ../../.; |
| 16 | + fileset = lib.fileset.fileFilter (file: file.hasExt "wiki") ../../pages; |
| 17 | + }; |
| 18 | + |
| 19 | + pageType = types.submodule { |
| 20 | + options = { |
| 21 | + title = mkOption { |
| 22 | + type = types.str; |
| 23 | + description = "Wiki page title"; |
| 24 | + }; |
| 25 | + namespace = mkOption { |
| 26 | + type = types.str; |
| 27 | + default = ""; |
| 28 | + description = "Wiki namespace (empty for main namespace)"; |
| 29 | + }; |
| 30 | + }; |
| 31 | + }; |
| 32 | +in |
| 33 | +{ |
| 34 | + options.services.nixos-wiki.pages = { |
| 35 | + |
| 36 | + pageConfig = mkOption { |
| 37 | + type = types.attrsOf pageType; |
| 38 | + default = { }; |
| 39 | + description = "Configuration for wiki pages, keyed by filename"; |
| 40 | + example = { |
| 41 | + "main-page.wiki" = { |
| 42 | + title = "Main Page"; |
| 43 | + namespace = ""; |
| 44 | + }; |
| 45 | + "help-editing.wiki" = { |
| 46 | + title = "Editing"; |
| 47 | + namespace = "Help"; |
| 48 | + }; |
| 49 | + }; |
| 50 | + }; |
| 51 | + }; |
| 52 | + |
| 53 | + config = mkIf (cfg.pageConfig != { }) { |
| 54 | + |
| 55 | + systemd.services.wiki-pages-sync = { |
| 56 | + description = "Synchronize wiki pages from git repository"; |
| 57 | + after = [ "mediawiki-init.service" ]; |
| 58 | + requiredBy = [ "multi-user.target" ]; |
| 59 | + serviceConfig = { |
| 60 | + Type = "oneshot"; |
| 61 | + User = "mediawiki"; |
| 62 | + Group = "nginx"; |
| 63 | + }; |
| 64 | + environment = config.services.phpfpm.pools.mediawiki.phpEnv; |
| 65 | + script = '' |
| 66 | + # Read the JSON config |
| 67 | + config='${builtins.toJSON cfg.pageConfig}' |
| 68 | +
|
| 69 | + # Process each .wiki file (they're in the pages subdirectory) |
| 70 | + for wiki_file in "${pagesDir}"/pages/*.wiki; do |
| 71 | + # Skip if no .wiki files found |
| 72 | + [ -e "$wiki_file" ] || continue |
| 73 | +
|
| 74 | + filename=$(basename "$wiki_file") |
| 75 | +
|
| 76 | + # Extract configuration for this file from JSON |
| 77 | + title=$(echo "$config" | ${pkgs.jq}/bin/jq -r --arg file "$filename" '.[$file].title // empty') |
| 78 | + namespace=$(echo "$config" | ${pkgs.jq}/bin/jq -r --arg file "$filename" '.[$file].namespace // ""') |
| 79 | +
|
| 80 | + # Skip if no config found for this file |
| 81 | + if [ -z "$title" ]; then |
| 82 | + echo "Warning: No configuration found for $filename, skipping" |
| 83 | + continue |
| 84 | + fi |
| 85 | +
|
| 86 | + # Construct full page title |
| 87 | + if [ -n "$namespace" ]; then |
| 88 | + full_title="$namespace:$title" |
| 89 | + else |
| 90 | + full_title="$title" |
| 91 | + fi |
| 92 | +
|
| 93 | + echo "Processing: $filename -> $full_title" |
| 94 | +
|
| 95 | + # Read page content and add management comment |
| 96 | + content=$(cat "$wiki_file") |
| 97 | + content_with_comment="<!-- |
| 98 | + This page is automatically managed through git repository synchronization. |
| 99 | + Do not edit this page directly on the wiki - changes will be overwritten. |
| 100 | + To edit this page, modify the file: $filename |
| 101 | + Source: https://github.com/NixOS/nixos-wiki-infra/blob/main/pages/$filename |
| 102 | + --> |
| 103 | +
|
| 104 | + $content" |
| 105 | +
|
| 106 | + # Edit the page using maintenance script (no --user means system maintenance user) |
| 107 | + echo "$content_with_comment" | ${config.services.phpfpm.pools.mediawiki.phpPackage}/bin/php \ |
| 108 | + ${config.services.mediawiki.finalPackage}/share/mediawiki/maintenance/run.php edit \ |
| 109 | + --summary="Updated $filename from git repository" \ |
| 110 | + --bot \ |
| 111 | + "$full_title" |
| 112 | +
|
| 113 | + echo "Successfully updated: $full_title" |
| 114 | + done |
| 115 | +
|
| 116 | + echo "Wiki synchronization completed successfully" |
| 117 | + ''; |
| 118 | + }; |
| 119 | + }; |
| 120 | +} |
0 commit comments