nix-configs

Personal NixOS and home-manager configuration files
Log | Files | Refs

stagit-generate.nix (5319B)


      1 { pkgs, username, ...}: let
      2   css = ''
      3     body,pre{font-family:monospace}
      4     #blob,article img{max-width:100%}
      5     html{font-size:12px;height:100%}
      6     body{margin:5rem auto;color:#aaa;background-color:#272727;width:66rem}
      7     pre{-moz-tab-size:4;tab-size:4}
      8     h1,h2,h3,h4,h5,h6{font-size:1em;margin:0}
      9     h1,h2,img{vertical-align:middle}
     10     img{border:0}
     11     a,a.d,a.h,a.i,a.line{color:#3498db;text-decoration:none}
     12     #blob{display:block;overflow-x:scroll}
     13     article.markup{font-size:15px;border:2px solid #00000017;border-radius:10px;font-family:sans-serif;padding:2.5em;margin:2em 0}
     14     article.markup code{font-size:.9em;border:1px solid #dbdbdb;background-color:#f7f7f7;padding:0 .3em;border-radius:.3em}
     15     article.markup pre code{border:none;background:0 0;padding:0;border-radius:0}
     16     article.markup pre{background-color:#f7f7f7;padding:1em;border:1px solid #dbdbdb;border-radius:.3em}
     17     article.markup h1{font-size:2.4em;padding-bottom:6px;border-bottom:5px solid #0000000a}
     18     article.markup h2{font-size:1.9em;padding-bottom:5px;border-bottom:2px solid #00000014}
     19     article.markup h3{font-size:1.5em}
     20     article.markup h4{font-size:1.3em}
     21     article.markup h5{font-size:1.1em}
     22     article.markup h6{font-size:1em}
     23     .linenos{margin-right:0;border-right:1px solid;user-select:none}
     24     .linenos a{margin-right:.9em;user-select:none;text-decoration:none}
     25     #blob a,.desc{color:#777}
     26     table thead td{font-weight:700}
     27     table td{padding:0 .4em}
     28     #content table td{vertical-align:top;white-space:nowrap}
     29     #branches tr:hover td,#files tr:hover td,#index tr:hover td,#log tr:hover td,#tags tr:hover td{background-color:#414141}
     30     #branches tr td:nth-child(3),#index tr td:nth-child(2),#log tr td:nth-child(2),#tags tr td:nth-child(3){white-space:normal}
     31     td.num{text-align:right}
     32     hr{border:0;border-top:1px solid #777;height:1px}
     33     .A,pre a.i,span.i{color:#29b74e}
     34     .D,pre a.d,span.d{color:#e42533}
     35     .url td:nth-child(2){padding-top:.2em;padding-bottom:.9em}
     36     .url td:nth-child(2) span{padding:1px 5px;background-color:#eee;border:1px solid #ddd;border-radius:5px}
     37     .url td:nth-child(2) span a{color:#444}
     38   '';
     39   stagit-generate = pkgs.writeShellScriptBin "stagit-generate" ''
     40     # variables
     41     source_directory="$HOME/vault/src"
     42     destination_directory="$HOME/docker/stagit"
     43     state_file="$destination_directory/.stagit-state"
     44 
     45     mkdir -p "$destination_directory"
     46 
     47     # state file
     48     if [ ! -f "$state_file" ]; then
     49       echo "# stagit-generate state file" > "$state_file"
     50       echo "# format: repo_path:last_commit_hash" >> "$state_file"
     51     fi
     52     get_latest_commit() {
     53       local repo_path="$1"
     54       cd "$repo_path" || return 1
     55       git rev-parse HEAD 2>/dev/null || echo "no-commits"
     56     }
     57     get_stored_commit() {
     58       local repo_name="$1"
     59       grep "^$repo_name:" "$state_file" 2>/dev/null | cut -d':' -f2- || echo ""
     60     }
     61     update_stored_commit() {
     62       local repo_name="$1"
     63       local commit_hash="$2"
     64       grep -v "^$repo_name:" "$state_file" > "$state_file.tmp" 2>/dev/null || touch "$state_file.tmp"
     65       echo "$repo_name:$commit_hash" >> "$state_file.tmp"
     66       mv "$state_file.tmp" "$state_file"
     67     }
     68 
     69     updated_repos=0
     70     skipped_repos=0
     71     index_needs_update=false
     72 
     73     # stagit loop
     74     for repo in $(find "$source_directory" -type d -name '.git' | sed 's|/\.git$||'); do
     75       repo_name=$(basename "$repo")
     76       output_directory="$destination_directory/$repo_name"
     77       current_commit=$(get_latest_commit "$repo")
     78       stored_commit=$(get_stored_commit "$repo_name")
     79 
     80       # repo update check
     81       if [ "$current_commit" != "$stored_commit" ] || [ ! -d "$output_directory" ]; then
     82         echo "Updating $repo_name... (was: $stored_commit, now: $current_commit)"
     83 
     84         mkdir -p "$output_directory"
     85         cd "$output_directory" || exit
     86 
     87         ${pkgs.stagit}/bin/stagit "$repo"
     88 
     89         cat > "style.css" <<EOF
     90         ${css}
     91 EOF
     92 
     93         # update state
     94         update_stored_commit "$repo_name" "$current_commit"
     95         updated_repos=$((updated_repos + 1))
     96         index_needs_update=true
     97       else
     98         echo "Skipping $repo_name (no changes since $stored_commit)"
     99         skipped_repos=$((skipped_repos + 1))
    100       fi
    101     done
    102 
    103     # re-generate index repositories were updated
    104     if [ "$index_needs_update" = true ]; then
    105       echo "Regenerating index..."
    106       cd "$destination_directory" || exit
    107       ${pkgs.stagit}/bin/stagit-index "''${source_directory}/"*/ >index.html
    108       cat > "style.css" <<EOF
    109       ${css}
    110 EOF
    111     else
    112       echo "Index unchanged, skipping re-generation"
    113     fi
    114 
    115     echo "Summary: Updated $updated_repos repositories, skipped $skipped_repos repositories"
    116   '';
    117 in {
    118   environment.systemPackages = [stagit-generate];
    119 
    120   # Systemd service and timer configuration
    121   systemd.services.stagit-generate = {
    122     serviceConfig = {
    123       Type = "oneshot";
    124       User = "${username}";
    125       ExecStart = "${stagit-generate}/bin/stagit-generate";
    126       StandardOutput = "journal";
    127       StandardError = "journal";
    128     };
    129   };
    130 
    131   systemd.timers.stagit-generate = {
    132     wantedBy = [ "timers.target" ];
    133     timerConfig = {
    134       OnCalendar = "*-*-* 00,04,08,12,16,20:00:00";
    135       RandomizedDelaySec = "15m";
    136       Persistent = true;
    137       AccuracySec = "1m";
    138     };
    139   };
    140 }