ctucx.git: nixfiles

ctucx' nixfiles

commit 208d4114c65a6abe96a9a2c36c4c21c9d4f15d9d
parent 63193b387424da3705bf255acc4ea107c4d6099c
Author: Leah (ctucx) <git@ctu.cx>
Date: Sun, 11 Dec 2022 11:31:12 +0100

machines/trabbi/websites/bikemap.ctu.cx: use tile-generator script from `gpx-map` flake
4 files changed, 49 insertions(+), 159 deletions(-)
M
flake.lock
|
58
+++++++++++++++++++++++++++++-----------------------------
M
flake.nix
|
12
++++++------
M
machines/trabbi/websites/bikemap.ctu.cx/default.nix
|
68
++++++++++++++------------------------------------------------------
D
machines/trabbi/websites/bikemap.ctu.cx/parse-gpx.pl
|
70
----------------------------------------------------------------------
diff --git a/flake.lock b/flake.lock
@@ -21,30 +21,6 @@
         "type": "github"
       }
     },
-    "bikemap": {
-      "inputs": {
-        "flake-utils": [
-          "flake-utils"
-        ],
-        "nixpkgs": [
-          "nixpkgs"
-        ]
-      },
-      "locked": {
-        "lastModified": 1670689983,
-        "narHash": "sha256-XlONLe9+H6bvOnanaS3F8+Q1xUwosu7ChYNkQALxMFA=",
-        "ref": "master",
-        "rev": "b1794683cb60057ebfead37ccf68bead0b6baf1f",
-        "revCount": 2,
-        "type": "git",
-        "url": "https://git.ctu.cx/bikemap"
-      },
-      "original": {
-        "ref": "master",
-        "type": "git",
-        "url": "https://git.ctu.cx/bikemap"
-      }
-    },
     "blobs": {
       "flake": false,
       "locked": {

@@ -155,7 +131,7 @@
         "type": "github"
       }
     },
-    "dns": {
+    "dns-nix": {
       "inputs": {
         "flake-utils": "flake-utils_2",
         "nixpkgs": [

@@ -169,12 +145,12 @@
         "rev": "1d6452f3c6fe65444a6115f49a5cf25724dee42d",
         "revCount": 73,
         "type": "git",
-        "url": "https://git.ctu.cx/dns-nix"
+        "url": "https://git.ctu.cx/dns.nix"
       },
       "original": {
         "ref": "master",
         "type": "git",
-        "url": "https://git.ctu.cx/dns-nix"
+        "url": "https://git.ctu.cx/dns.nix"
       }
     },
     "flake-compat": {

@@ -263,6 +239,30 @@
         "url": "https://git.ctu.cx/flauschehorn.sexy"
       }
     },
+    "gpx-map": {
+      "inputs": {
+        "flake-utils": [
+          "flake-utils"
+        ],
+        "nixpkgs": [
+          "nixpkgs"
+        ]
+      },
+      "locked": {
+        "lastModified": 1670755287,
+        "narHash": "sha256-OgwDS+KkicpZwF8w5JHqiQuCgKEFHWni72OIb4VjPq4=",
+        "ref": "master",
+        "rev": "b94fc23afb7cb8f5c67c7b95751ae12d9c807cf9",
+        "revCount": 3,
+        "type": "git",
+        "url": "https://git.ctu.cx/gpx-map"
+      },
+      "original": {
+        "ref": "master",
+        "type": "git",
+        "url": "https://git.ctu.cx/gpx-map"
+      }
+    },
     "home-manager": {
       "inputs": {
         "nixpkgs": [

@@ -525,14 +525,14 @@
     "root": {
       "inputs": {
         "agenix": "agenix",
-        "bikemap": "bikemap",
         "colmena": "colmena",
         "ctucx-gallery": "ctucx-gallery",
         "ctucx-things": "ctucx-things",
         "darwin": "darwin",
-        "dns": "dns",
+        "dns-nix": "dns-nix",
         "flake-utils": "flake-utils_3",
         "flauschehorn-sexy": "flauschehorn-sexy",
+        "gpx-map": "gpx-map",
         "home-manager": "home-manager",
         "lacrosse2mqtt": "lacrosse2mqtt",
         "local-secrets": "local-secrets",
diff --git a/flake.nix b/flake.nix
@@ -7,7 +7,7 @@
       (import ./pkgs/overlay.nix)
 
       (final: prev: {
-        dns = inputs.dns;
+        dns      = inputs.dns-nix;
         unstable = inputs.nixpkgsUnstable.legacyPackages.${prev.system};
       })
 

@@ -16,7 +16,7 @@
 
       inputs.stagit.overlay
 
-      inputs.bikemap.overlay
+      inputs.gpx-map.overlay
       inputs.mobile-coverage-map.overlay
 
       inputs.sdm2mqtt.overlay

@@ -137,9 +137,9 @@
       ref   = "master";
     };
 
-    dns = {
+    dns-nix = {
       type  = "git";
-      url   = "https://git.ctu.cx/dns-nix";
+      url   = "https://git.ctu.cx/dns.nix";
       ref   = "master";
       inputs.nixpkgs.follows = "nixpkgs";
     };

@@ -224,9 +224,9 @@
       inputs.flake-utils.follows = "flake-utils";
     };
 
-    bikemap = {
+    gpx-map = {
       type  = "git";
-      url   = "https://git.ctu.cx/bikemap";
+      url   = "https://git.ctu.cx/gpx-map";
       ref   = "master";
       inputs.nixpkgs.follows     = "nixpkgs";
       inputs.flake-utils.follows = "flake-utils";
diff --git a/machines/trabbi/websites/bikemap.ctu.cx/default.nix b/machines/trabbi/websites/bikemap.ctu.cx/default.nix
@@ -1,48 +1,6 @@
 { config, pkgs, ... }:
 
 let
-  parse-gpx = pkgs.stdenv.mkDerivation rec {
-    name = "parse-gpx";
-    src  = ./parse-gpx.pl;
-
-    dontUnpack = true;
-
-    nativeBuildInputs = [ pkgs.makeWrapper ];
-    buildInputs       = [ pkgs.perl ];
-
-    installPhase = ''mkdir -p $out/bin; cp $src $out/bin/parse-gpx; chmod +x $out/bin/parse-gpx;'';
-    postFixup    = ''wrapProgram $out/bin/parse-gpx --prefix PERL5LIB : "${with pkgs.perlPackages; makePerlPath [ XMLParser ]}"'';
-  };
-
-  datamaps  = pkgs.stdenv.mkDerivation rec {
-    name = "datamaps";
-
-    src = pkgs.fetchFromGitHub {
-      owner  = "e-n-f";
-      repo   = "datamaps";
-      rev    = "76e620adabbedabd6866b23b30c145b53bae751e";
-      sha256 = "1rdqbyfmgidiv4aqy1s6llls304dxbg5226c7k622smd2rnda2jk";
-    };
-
-    buildInputs = with pkgs; [ pkgconfig libpng ];
-
-    installPhase = ''
-      mkdir -p $out/bin;
-      cp {encode,render,merge,enumerate} $out/bin;
-    '';
-  };
-
-  makeTile = pkgs.writeShellScript "makeTile" ''
-    mkdir -p tiles/$2/$3
-    echo "rendering $1 $2 $3 $4 $5 $6"
-
-    if [ $2 -gt 13 ]
-    then 
-      ${datamaps}/bin/render -g -t0 -L4 -c 'ff8800' -S 'ff8800' $1 $2 $3 $4 | ${pkgs.pngquant}/bin/pngquant 256 > tiles/$2/$3/$4.png
-    else 
-      ${datamaps}/bin/render -g -t0 -L7 -c 'ff8800' -S 'ff8800' $1 $2 $3 $4 | ${pkgs.pngquant}/bin/pngquant 256 > tiles/$2/$3/$4.png
-    fi
-  '';
 
   deployScript = pkgs.writeShellScript "deploy" ''
     systemctl start deploy-bikemap;

@@ -71,22 +29,25 @@ in {
   systemd = {
     services.deploy-bikemap = {
       script = ''
-        tmpdir=$(mktemp -d);
-        cd $tmpdir
+        # strict mode
+        set -euo pipefail
+        IFS=$'\n\t'
 
-        ${pkgs.git}/bin/git clone /var/lib/gitolite/repositories/biketracks.git $tmpdir/tracks;
+        TMP_DIR=$(mktemp -d)
+        trap "{ rm -rf "$TMP_DIR"; }" SIGINT SIGTERM ERR EXIT
 
-        find $tmpdir/tracks -name '*.gpx' -print0 | xargs -0 ${parse-gpx}/bin/parse-gpx | ${datamaps}/bin/encode -z16 -m8 -o $tmpdir/gpx.dm
-        ${datamaps}/bin/enumerate -s -Z6 -z16 ./gpx.dm | xargs -L1 -P3 ${makeTile}
+        ${pkgs.git}/bin/git clone /var/lib/gitolite/repositories/biketracks.git $TMP_DIR/tracks
 
-        rm -rf ~/*;
+        mkdir $TMP_DIR/tiles
 
-        ln -sf ${pkgs.bikemap}/index.html ~/index.html;
-        ln -sf ${pkgs.bikemap}/bundle.js  ~/bundle.js;
-        cp -r  $tmpdir/tiles              ~/tiles;
-        echo "{\"lastUpdated\":\"$(date +"%Y-%m-%d %H:%M")\"}" > ~/lastUpdated.json;
+        ${pkgs.generateTilesFromGPX}/bin/generateTilesFromGPX $TMP_DIR/tracks $TMP_DIR/tiles
+
+        rm -rf ~/*;
 
-        rm -rf $tmpdir;
+        ln -sf ${pkgs.gpx-map}/index.html ~/index.html
+        ln -sf ${pkgs.gpx-map}/bundle.js  ~/bundle.js
+        mv     $TMP_DIR/tiles             ~/tiles;
+        echo "{\"lastUpdated\":\"$(date +"%Y-%m-%d %H:%M")\"}" > ~/lastUpdated.json
       '';
 
       serviceConfig = {

@@ -121,7 +82,6 @@ in {
   services = {
     gitolite.hooks.postReceive = ''
       #deploy bikemap
-      [ "$GL_REPO" == "bikemap" ] && sudo ${deployScript}
       [ "$GL_REPO" == "biketracks" ] && sudo ${deployScript}
     '';
 
diff --git a/machines/trabbi/websites/bikemap.ctu.cx/parse-gpx.pl b/machines/trabbi/websites/bikemap.ctu.cx/parse-gpx.pl
@@ -1,70 +0,0 @@
-#!/usr/bin/env perl
-use POSIX;
-use XML::Parser;
-
-$pi = 4 * atan2(1, 1);
-
-sub handle_start {
-	my ($expat, $element, @tags) = @_;
-	my %tags = @tags;
-
-	if ($element eq "trkpt") {
-		$lat = $tags{'lat'};
-		$lon = $tags{'lon'};
-	} elsif ($element eq "ele") {
-		$state = "ele";
-	} elsif ($element eq "time") {
-		$state = "time";
-	} else {
-		$state = "";
-	}
-}
-
-sub handle_char {
-	my ($expat, $string) = @_;
-
-	$text{$state} .= $string;
-}
-
-sub handle_end {
-	my ($expat, $element) = @_;
-
-	if ($element eq "trkseg" || $element eq "trk") {
-		$oldlat = $oldlon = "";
-	} elsif ($element eq "trkpt") {
-		if ($lat ne "" && $lon ne "" && $oldlat ne "" && $oldlon ne "") {
-			if ($lat ne $oldlat || $lon ne $oldlon) {
-				print "$oldlat,$oldlon $lat,$lon ";
-
-				$rat = cos(($lat + $oldlat) / 2 * $pi / 180);
-				$ang = atan2($lat - $oldlat, ($lon - $oldlon) * $rat);
-
-				printf("8:%d\n", ($ang + $pi) * 256 / (2 * $pi));
-			}
-		}
-
-		$oldlat = $lat;
-		$oldlon = $lon;
-		%text = ();
-	}
-}
-
-if ($#ARGV < 0) {
-	$parser = new XML::Parser(Handlers => { Start => \&handle_start,
-						End   => \&handle_end,
-						Char  => \&handle_char,
-					      });
-
-	$parser->parse(*STDIN);
-} else {
-	for $file (@ARGV) {
-		$oldwhen = 0;
-
-		$parser = new XML::Parser(Handlers => { Start => \&handle_start,
-							End   => \&handle_end,
-							Char  => \&handle_char,
-						      });
-
-		$parser->parsefile($file);
-	}
-}