{
  description = "A very basic flake";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };

  inputs.utils.url = "github:numtide/flake-utils";

  outputs = { self, nixpkgs, utils, ... }: 

  nixpkgs.lib.attrsets.recursiveUpdate 
  (utils.lib.eachSystem (utils.lib.defaultSystems) ( system:
  let
    pkgs = nixpkgs.legacyPackages.${system};
  in rec
  {
    devShells.default = pkgs.mkShell {
      packages = with pkgs; [
        go
        gotools
        poppler_utils #get first pdf page to png
        tailwindcss
      ];
    };

    packages.zineshop = nixpkgs.legacyPackages.x86_64-linux.buildGoModule {
      pname = "zineshop";
      version = "1.0";
      vendorHash = "sha256-0M/xblZXVw4xIFZeDewYrFu7VGUCsPTPG13r9ZpTGJo=";
      src = ./.;

      postInstall = ''
        cp -r views $out/
        cp -r static $out/
        '';
    };

    packages.default = packages.zineshop;

    checks = let
      checkArgs = {
        pkgs = pkgs;
        inherit self;
      };
    in {
      zineshop = import ./test/test.nix checkArgs;
    };

    })) {
    
    nixosModules.zineshop = { config, lib, pkgs, ... }:
      let
        cfg = config.services.zineshop;
        zineshop-pkg = self.packages.x86_64-linux.zineshop;
      in
      {
        options = {
          services.zineshop = {
            enable = lib.mkOption {
              default = false;
              type = lib.types.bool;
              description = lib.mdDoc ''
                Enables zineshop
                '';
            };
          };
        };

        config = lib.mkIf cfg.enable {
          environment.systemPackages = [ 
            zineshop-pkg
            pkgs.poppler_utils #get first pdf page to png
          ];

          systemd.tmpfiles.rules = [
            "d '/var/lib/zineshop' 0750 zineshop zineshop - -"
            "d '/var/lib/zineshop/views' 0750 zineshop zineshop - -"
            "d '/var/lib/zineshop/static' 0750 zineshop zineshop - -"
            "d '/var/lib/zineshop/static/uploads' 0750 zineshop zineshop - -"
            "z '/var/lib/zineshop' 0750 zineshop zineshop - -"
            "z '/var/lib/zineshop/views' 0750 zineshop zineshop - -"
            "z '/var/lib/zineshop/static' 0750 zineshop zineshop - -"
            "z '/var/lib/zineshop/static/uploads' 0750 zineshop zineshop - -"
          ];

          users = {
            groups.zineshop = {};
            users.zineshop = {
              description = "zineshop user";
              group = "zineshop";
              isNormalUser = true;
            };
          };

          systemd.services.zineshop = {
            description = "zineshop daemon";
            serviceConfig = {
              Type = "simple";
              WorkingDirectory = "/var/lib/zineshop";
              ExecStart = pkgs.writeScript "start-zineshop" ''
                #! ${pkgs.bash}/bin/bash
                PATH="$PATH:${lib.makeBinPath [ pkgs.poppler_utils ]}"
                ${zineshop-pkg}/bin/zineshop
              '';
              Restart = "on-failure";
            };

            environment = {
              SQLITE_DB = "/var/lib/zineshop/zineshop.db";
              SECRET = "secretforjwt"; #TODO: BAD!
              PORT = "8080";
              STATIC = "/var/lib/zineshop/static";
              VIEWS = "/var/lib/zineshop/views";
            };

            preStart = ''
              mkdir -m 0770 -p "/var/lib/zineshop"
              cp -r ${zineshop-pkg}/views /var/lib/zineshop/
              cp -r ${zineshop-pkg}/static /var/lib/zineshop/
              chown zineshop:zineshop "/var/lib/zineshop"
            '';

            wantedBy = [ "default.target" ];

            environment = {
              USER = "zineshop";
              HOME = "/var/lib/zineshop";
            };
          };
        };
      };

    };
}
