<commit_before>{ buildGoPackage, fetchpatch, stdenv, lib, procps, fetchFromGitHub }:

let
  common = { stname, target, postInstall ? "" }:
    buildGoPackage rec {
      version = "1.0.0";
      name = "${stname}-${version}";

      src = fetchFromGitHub {
        owner  = "syncthing";
        repo   = "syncthing";
        rev    = "v${version}";
        sha256 = "1qkjnij9jw3d4pjkdr6npz5ps604qg6g36jnsng0k1r2qnrydnwh";
      };

      goPackagePath = "github.com/syncthing/syncthing";

      patches = [
        ./add-stcli-target.patch
      ];
      BUILD_USER="nix";
      BUILD_HOST="nix";

      buildPhase = ''
        runHook preBuild
        pushd go/src/${goPackagePath}
        go run build.go -no-upgrade -version v${version} build ${target}
        popd
        runHook postBuild
      '';

      installPhase = ''
        pushd go/src/${goPackagePath}
        runHook preInstall
        install -Dm755 ${target} $bin/bin/${target}
        runHook postInstall
        popd
      '';

      inherit postInstall;

      meta = with lib; {
        homepage = https://www.syncthing.net/;
        description = "Open Source Continuous File Synchronization";
        license = licenses.mpl20;
        maintainers = with maintainers; [ <NAME> peterhoeg andrew-d ];
        platforms = platforms.unix;
      };
    };

in {
  syncthing = common {
    stname = "syncthing";
    target = "syncthing";

    postInstall = ''
      # This installs man pages in the correct directory according to the suffix
      # on the filename
      for mf in man/*.[1-9]; do
        mantype="$(echo "$mf" | awk -F"." '{print $NF}')"
        mandir="$out/share/man/man$mantype"
        install -Dm644 "$mf" "$mandir/$(basename "$mf")"
      done

    '' + lib.optionalString (stdenv.isLinux) ''
      mkdir -p $out/lib/systemd/{system,user}

      substitute etc/linux-systemd/system/syncthing-resume.service \
                 $out/lib/systemd/system/syncthing-resume.service \
                 --replace /usr/bin/pkill ${procps}/bin/pkill

      substitute etc/linux-systemd/system/syncthing@.service \
                 $out/lib/systemd/system/syncthing@.service \
                 --replace /usr/bin/syncthing $bin/bin/syncthing

      substitute etc/linux-systemd/user/syncthing.service \
                 $out/lib/systemd/user/syncthing.service \
                 --replace /usr/bin/syncthing $bin/bin/syncthing
    '';
  };

  syncthing-cli = common {
    stname = "syncthing-cli";

    target = "stcli";
  };

  syncthing-discovery = common {
    stname = "syncthing-discovery";
    target = "stdiscosrv";
  };

  syncthing-relay = common {
    stname = "syncthing-relay";
    target = "strelaysrv";

    postInstall = lib.optionalString (stdenv.isLinux) ''
      mkdir -p $out/lib/systemd/system<commit_msg>syncthing: move systemd files to bin output<commit_after>{ buildGoPackage, fetchpatch, stdenv, lib, procps, fetchFromGitHub }:

let
  common = { stname, target, postInstall ? "" }:
    buildGoPackage rec {
      version = "1.0.0";
      name = "${stname}-${version}";

      src = fetchFromGitHub {
        owner  = "syncthing";
        repo   = "syncthing";
        rev    = "v${version}";
        sha256 = "1qkjnij9jw3d4pjkdr6npz5ps604qg6g36jnsng0k1r2qnrydnwh";
      };

      goPackagePath = "github.com/syncthing/syncthing";

      patches = [
        ./add-stcli-target.patch
      ];
      BUILD_USER="nix";
      BUILD_HOST="nix";

      buildPhase = ''
        runHook preBuild
        pushd go/src/${goPackagePath}
        go run build.go -no-upgrade -version v${version} build ${target}
        popd
        runHook postBuild
      '';

      installPhase = ''
        pushd go/src/${goPackagePath}
        runHook preInstall
        install -Dm755 ${target} $bin/bin/${target}
        runHook postInstall
        popd
      '';

      inherit postInstall;

      meta = with lib; {
        homepage = https://www.syncthing.net/;
        description = "Open Source Continuous File Synchronization";
        license = licenses.mpl20;
        maintainers = with maintainers; [ p<NAME> peterhoeg andrew-d ];
        platforms = platforms.unix;
      };
    };

in {
  syncthing = common {
    stname = "syncthing";
    target = "syncthing";

    postInstall = ''
      # This installs man pages in the correct directory according to the suffix
      # on the filename
      for mf in man/*.[1-9]; do
        mantype="$(echo "$mf" | awk -F"." '{print $NF}')"
        mandir="$out/share/man/man$mantype"
        install -Dm644 "$mf" "$mandir/$(basename "$mf")"
      done

    '' + lib.optionalString (stdenv.isLinux) ''
      mkdir -p $bin/lib/systemd/{system,user}

      substitute etc/linux-systemd/system/syncthing-resume.service \
                 $bin/lib/systemd/system/syncthing-resume.service \
                 --replace /usr/bin/pkill ${procps}/bin/pkill

      substitute etc/linux-systemd/system/syncthing@.service \
                 $bin/lib/systemd/system/syncthing@.service \
                 --replace /usr/bin/syncthing $bin/bin/syncthing

      substitute etc/linux-systemd/user/syncthing.service \
                 $bin/lib/systemd/user/syncthing.service \
                 --replace /usr/bin/syncthing $bin/bin/syncthing
    '';
  };

  syncthing-cli = common {
    stname = "syncthing-cli";

    target = "stcli";
  };

  syncthing-discovery = common {
    stname = "syncthing-discovery";
    target = "stdiscosrv";
  };

  syncthing-relay = common {
    stname = "syncthing-relay";
    target = "strelaysrv";

    postInstall = lib.optionalString (stdenv.isLinux) ''
      mkdir -p $out/lib/systemd/system
{'fraction_non_alphanumeric': 0.13069693208015606, 'fraction_numerical': 0.010994857244192233, 'mean_word_length': 2.1350750416898276, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5994634', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1841, 'n_words': 351}