diff --git a/internal/config/config.go b/internal/config/config.go index a110d82d..496e68fd 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -61,9 +61,10 @@ func Read(path string) (config types.Configuration, err error) { func MkGitConfig(config types.Configuration) types.GitConfig { return types.GitConfig{ - Path: filepath.Join(config.StateDir, "repository"), - Dir: config.FlakeSubdirectory, - Remotes: config.Remotes, - GpgPublicKeyPaths: config.GpgPublicKeyPaths, + Path: filepath.Join(config.StateDir, "repository"), + Dir: config.FlakeSubdirectory, + Remotes: config.Remotes, + GpgPublicKeyPaths: config.GpgPublicKeyPaths, + AllowForcePushMain: config.AllowForcePushMain, } } diff --git a/internal/repository/git.go b/internal/repository/git.go index 4c20ba1e..69661539 100644 --- a/internal/repository/git.go +++ b/internal/repository/git.go @@ -85,7 +85,11 @@ func getHeadFromRemoteAndBranch(r repository, remoteName, branchName, currentMai } if err = hasNotBeenHardReset(r, branchName, currentMainHash, head); err != nil { - return + if r.GitConfig.AllowForcePushMain { + logrus.Infof("Force-push detected but ignored due to 'allowForcePushMain' being set") + } else { + return + } } commitObject, err := r.Repository.CommitObject(*head) diff --git a/internal/types/types.go b/internal/types/types.go index 804ea58a..93681cd8 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -18,9 +18,10 @@ type GitConfig struct { // The repository Path Path string // The directory in the repository - Dir string - Remotes []Remote - GpgPublicKeyPaths []string + Dir string + Remotes []Remote + GpgPublicKeyPaths []string + AllowForcePushMain bool } type Auth struct { @@ -59,4 +60,5 @@ type Configuration struct { Exporter HttpServer `yaml:"exporter"` GpgPublicKeyPaths []string `yaml:"gpg_public_key_paths"` PostDeploymentCommand string `yaml:"post_deployment_command"` + AllowForcePushMain bool `yaml:"allow_force_push_main"` } diff --git a/nix/module-options.nix b/nix/module-options.nix index e7b7cb74..3e8a36be 100644 --- a/nix/module-options.nix +++ b/nix/module-options.nix @@ -1,5 +1,11 @@ -{ config, pkgs, lib, ... }: { - options = with lib; with types; { +{ + config, + pkgs, + lib, + ... +}: { + options = with lib; + with types; { services.comin = { enable = mkOption { type = types.bool; @@ -8,9 +14,11 @@ Whether to run the comin service. ''; }; - package = lib.mkPackageOption pkgs "comin" { nullable = true; } // { - defaultText = "pkgs.comin or comin.packages.\${system}.default or null"; - }; + package = + lib.mkPackageOption pkgs "comin" {nullable = true;} + // { + defaultText = "pkgs.comin or comin.packages.\${system}.default or null"; + }; hostname = mkOption { type = str; default = config.networking.hostName; @@ -189,6 +197,11 @@ pkgs.writers.writeBash "post" "echo $COMIN_GIT_SHA"; ''; }; + allowForcePushMain = mkOption { + description = "Switch to configuration even when a force-push was detected on main."; + type = bool; + default = false; + }; }; }; } diff --git a/nix/module.nix b/nix/module.nix index 05988655..e09ac660 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -1,8 +1,20 @@ { self }: { config, pkgs, lib, ... }: let cfg = config; - cominConfigLib = import ./comin-config.nix { inherit config pkgs lib; }; - inherit (cominConfigLib) cominConfig cominConfigYaml; + yaml = pkgs.formats.yaml { }; + cominConfig = { + hostname = cfg.services.comin.hostname; + state_dir = "/var/lib/comin"; + flake_subdirectory = cfg.services.comin.flakeSubdirectory; + remotes = cfg.services.comin.remotes; + exporter = { + listen_address = cfg.services.comin.exporter.listen_address; + port = cfg.services.comin.exporter.port; + }; + gpg_public_key_paths = cfg.services.comin.gpgPublicKeyPaths; + allow_force_push_main = cfg.services.comin.allowForcePushMain; + }; + cominConfigYaml = yaml.generate "comin.yaml" cominConfig; inherit (pkgs.stdenv.hostPlatform) system; inherit (cfg.services.comin) package;