{ pkgs, lib, ...}: 
let
  globals = import ../../config/globals.nix ;
in {
  environment.systemPackages = with pkgs; [
    hugo
  ];
  systemd.services.hugo-build = {
    description = "Auto build du blog hugo";
    serviceConfig = {
      Type = "oneshot";
      WorkingDirectory = globals.services.levr.home ;
      ExecStart = ''${pkgs.hugo}/bin/hugo --minify build -d ${globals.services.levr.build}'';
      User = "levr";
    };
  };
  systemd.timers.hugo-build = {
    description = "Timer pour rebuild du blog";
    wantedBy = [ "timers.target" ];
    timerConfig = {
      OnCalendar = "01:10" ;
      Persistent = true ;
    };
  };
  systemd.services.blog-sync = {
    description = "Synchronisation du dépôt Hugo";
    serviceConfig = {
      Type = "oneshot";
      WorkingDirectory = globals.services.levr.home;
      ExecStart = "${pkgs.git}/bin/git pull origin master";
      User = "levr";
    };
    environment = {
      PATH = lib.mkForce "${pkgs.openssh}/bin";
    };
  };
  systemd.timers.blog-sync = {
    wantedBy = [ "timers.target" ];
    timerConfig = {
      OnCalendar = "01:00";
      Persistent = true;
    };
  };
  services.caddy = {
    enable = true;
    virtualHosts = {
      "${globals.services.levr.url}" = {
        extraConfig = ''
          @http {
            protocol http
          }
        redir @http https://{host}{uri} permanent
          root * ${globals.services.levr.build}
          file_server

          log {
            output file /var/log/caddy/access-${globals.services.levr.url}.log
          }
        '';
      };
    };
  };
  users = {
    users = {
      "${globals.services.levr.user}" = {
        isSystemUser = true ;
        group = globals.services.levr.user ;
        home = globals.services.levr.home ;
        createHome = true ;
        description = "User for hugo-blog builds and deployments";
      };
    };
    groups.${globals.services.levr.user} = {};
  };

  systemd.tmpfiles.rules = [
    "d ${globals.services.levr.home} 0755 levr levr -"
    "d ${globals.services.levr.build} 0755 levr levr -"
  ];
}
