Stable NixOS with unstable Gitea?

NixOS 26.05 uses Gitea release 1.27.3, while the unstable NixOS branch has switched to Gitea 28.1. This latest Gitea release offers many bug fixes and features I have been waiting for, so I wonder if anybody here has tried combining the latest Gitea with NixOS 26.05? If so, what are your experiences, and how exactly did you do it?

I am aware of the services.gitea.package option, but I am not sure how to use it in this case.

Do you use flakes or some other way to manage your system inputs and do you already use unstable packages?

edit remember that 26.11 will be released in about 6 weeks, maybe just wait?

@NobbZ I should have mentioned that I use Flakes, sorry. The machine in question currently only uses stable packages. Gitea in its latest incarnation is however interesting enough to me that I’d be willing to mix a stable base system with “unstable” (at least in NixOS terms) Gitea. Interesting enough to not want to wait, even.

You have a few choices. Personally, i’d just override the sources in stable with an assertion to only do it for 26.05 or older. You can also pull in unstable as an flake input, and just set the gitea option to your unstable input.

That’d look something like this:

# configuration.nix
{ lib, pkgs, ... }: {
  services.gitea.package = lib.mkIf (lib.versionOlder pkgs.gitea "28.1.0") (
    pkgs.gitea.overrideAttrs (
      drv: old: {
        # You'll have to manually update this number for security
        # fixes until you upgrade to 26.11
        version = "28.1.0";
        src = old.src.override {
          tag = "v${drv.version}";

          # REMEMBER to update this if you bump versions; otherwise nix won't
          # actually update the source but still give you an "updated" gitea
          # version number.
          hash = pkgs.lib.fakeHash;
        };
      }
    )
  );
}

This might or might not work, depending on how many changes there were to gitea’s build scripts and dependencies. If it doesn’t work OOTB you can probably fix it, but that’ll require understanding how to build packages with nix a bit.

I’m not a huge fan of recommending that approach because I don’t believe the kind of person who asks for help with this has the contextual knowledge to update properly, including bumping the source hash. But big ol’ comments hopefully go a long way, and @rseichter sounds like enough of a sysadmin to be able to follow those instructions :wink:

It’s probably the cleanest solution when it works and is done correctly. Still runs the risk of not being able to update quickly when a critical CVE is patched but the build script needs updating when you have zero context for how the upstream package works, but I’ll leave that risk assessment to you.

The alternative is something like:

# flake.nix
{
  inputs = {
    nixpkgs.url = "https://channels.nixos.org/nixos-26.05/nixexprs.tar.xz";
    nixpkgs-unstable.url = "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz";
  };

  outputs = { nixpkgs, ... }@inputs: {
    nixosConfigurations.nixos = nixpkgs.lib.nixosSystem {
      specialArgs = { inherit inputs; };
      configuration = [ ./configuration.nix ];
    };
  };
}

And then one of these:

# configuration.nix
{ inputs, pkgs, ... }: {
  services.gitea.package = pkgs.callPackage "${inputs.nixpkgs-unstable}/pkgs/by-name/gi/gitea/package.nix" { };
}

or

# configuration.nix
{ inputs, pkgs, ... }: {
  services.gitea.package = inputs.nixpkgs-unstable.legacyPackages.${pkgs.stdenv.hostPlatform.system}.gitea;
}

The benefit of the former is that you’ll be linking against libraries - and using other dependencies - from nixpkgs stable. This means that you’ll have way more integration and duplicate less stuff. The downside is that the build was never actually tested with those versions so it might not work. Unlike the .overrideAttrs solution the build script will be correct though.

The latter will always work, but has the least good integration with your system, I usually treat it as a last resort.

Thank you for sharing this variety of options with me. Your comments inspired me to rethink the way I approach Gitea on NixOS in general. I don’t even use services.gitea yet, but an overlay, and your suggestions look more elegant.

Wont. gitea is a go program, and therefore requires also overriding of the vendorerd deps.

This doesn’t fully make sense to me. If you use gitea from an overlay, then you have to make sure it gets updated in that overlay. Also how do you setup gitea if not using the module? That totally changes how to solve this!

I know that’s needed for rust, but it changed and started building new vendored deps when I tried it :person_shrugging: Maybe it’s a case of updating vendorHash when nix complains?

Patches failed to apply though, so yeah, it won’t actually work work.

@NobbZ Here’s how I am running Gitea as a service, without using services.gitea.* to do it. This approach is old, from a time when I knew even less about NixOS than I do today. The accompanying script gitea.sh (not pictured) takes care of passing certain arguments like --work-path to the binary. This approach also allows me to use a custom Gitea app.ini file which NixOS does not meddle with.

systemd.services."gitea-sh" = {
  description = "Gitea Web Service (gitea.sh)";
  serviceConfig = {
    ExecStart = "${pkgs.gitea-sh-app}/bin/gitea.sh web";
    RemainAfterExit = "yes";
    Type = "oneshot";
    User = "git";
  };
  wantedBy = [ "network-online.target" ];
};

nixpkgs.overlays = [
  (final: prev: {
    gitea-sh-app = final.writeShellApplication {
      name = "gitea.sh";
      runtimeInputs = [
        final.bash
        final.git
        final.gitea
      ];
      text = builtins.readFile ./gitea.sh;
    };
  })
];

These days, services.gitea looks like a better approach to me. I am not sure if that service option existed back in the olden days when I first deployed Gitea to NixOS.

This would have been relevant at the beginning, as here, the places where you put overriden or changed gitea drvs is affected by this choice.

In my opinion you should migrate to the module, if you can do so without loosing your data.

And only then you should see how to upgrade to the newer gitea.

I’m sorry, it was not my intention to mislead you guys. You are also correct in suggesting that I don’t try to both migrate to the service module and to a newer Gitea relase at the same time.

By the way, moving from final.gitea to pkgsUnstable.gitea in the above overlay, combined with a matching flake input, seems to work in preliminary tests. Too early to consider for a production setup, but a good start nonetheless.

I would really suggest not using overlays like that. There’s no reason to mutate pkgs unless you actually want to change something in pkgs. Also very weird to use a oneshot unit to start a daemon.

I suspect a bunch of varied misunderstandings lead to that snippet - but yeah, just use the NixOS modules wherever possible, they tend to be pretty clean.

That’s a kind way of putting it, thanks. :wink: Well, now to tackle the Gitea data migration process. That might take some doing, what with how Gitea itself and the NixOS Gitea module handle the necessary encryption keys, respectively.