aboutsummaryrefslogblamecommitdiff
path: root/flake.nix
blob: b559c45bdabbfbcf906133006813bce0f05aa7fd (plain) (tree)
































































































                                                                                                                                                
{
  inputs = {
    flake-utils.url = "github:numtide/flake-utils";
    naersk.url = "github:nix-community/naersk";
  };

  outputs = { self, nixpkgs, flake-utils, naersk }:
    flake-utils.lib.eachDefaultSystem (
      system: let
        pkgs = nixpkgs.legacyPackages."${system}";
        naersk-lib = naersk.lib."${system}";
      in
        rec {
          # `nix build`
          deps = with pkgs; [
            pkg-config
            openssl
            gcc
          ];
          packages.glitch-ng = naersk-lib.buildPackage {
            pname = "glitch-ng";
            root = ./.;
            buildInputs = deps;
          };
          defaultPackage = packages.glitch-ng;

          # `nix run`
          apps.glitch-ng = flake-utils.lib.mkApp {
            drv = packages.glitch-ng;
          };
          defaultApp = apps.glitch-ng;

          nixosModules.glitch = { config, lib, ... }: {
            options = {
              services.glitch-ng.enable = lib.mkEnableOption "enable glitch NG";
              services.glitch-ng.environment-file-location = lib.mkOption {
                type = lib.types.path;
                default = "/var/lib/glitch-ng/.env";
                description = "The location of the environment file";
              };
            };

            config = lib.mkIf config.services.glitch-ng.enable {
              users.groups.glitch-ng = {
                members = [ "glitch-ng" ];
              };
              users.users.glitch-ng = {
                createHome = true;
                isSystemUser = true;
                home = "/var/lib/glitch-ng";
                group = "glitch-ng";
              };

              systemd.services.glitch-ng = {
                wantedBy = [ "multi-user.target" ];
                after = [ "glitch-ng-init.service" ];
                requires = [ "glitch-ng-init.service" ];
                serviceConfig = {
                  User = "glitch-ng";
                  Group = "glitch-ng";
                  Restart = "always";
                  WorkingDirectory = "${defaultPackage}";
                  ExecStart = "${defaultPackage}/bin/glitch-ng";
                  EnvironmentFile = "${config.services.glitch-ng.environment-file-location}";
                };
              };

              systemd.services.glitch-ng-init = {
                wantedBy = [ "multi-user.target" ];
                requires =  [ "postgresql.service" ];
                after =  [ "postgresql.service" ];
                description = "Initialize for glitch-ng";

                script = with pkgs; ''
                  if ! [ -e /var/lib/glitch-ng/.db-created ]; then
                    runuser -u ${config.services.postgresql.superUser} -- ${config.services.postgresql.package}/bin/createuser glitch-ng
                    runuser -u ${config.services.postgresql.superUser} -- ${config.services.postgresql.package}/bin/createdb -O glitch-ng glitch
                    touch /var/lib/glitch-ng/.db-created
                  fi
                '';
                serviceConfig = {
                  Type = "oneshot";
                };
              };

              services.postgresql.enable = true;

            };
          };

          # `nix develop`
          devShell = pkgs.mkShell {
            nativeBuildInputs = with pkgs; [ rustc cargo ] ++ deps;
          };
        }
    );
}