Creating a Nix Flake to package an application and systemd service (as a NixOS Module)

Hi there!

I’m trying to create a Flake that does the following:

  • builds and packages a Rust executable
  • exposes a NixOS module that provides a systemd service to run that executable (In my real project, it’s an HTTP server, but for this example it’s just a simple ‘Hello world!’).

I think I have managed the first step, but I’m struggling with the second step.

I’ve cut down the size of my project and reduced it to this minimum example: experiments/hallo-nix-flakes: Attempt to create a flake - hallo-nix-flakes - Gitea and biscuits

My biggest problem so far is that I don’t know what to specify as the entry point (in experiments/hallo-nix-flakes: Attempt to create a flake - nix_flake/modules/halloSvc.nix at master - hallo-nix-flakes - Gitea and biscuits); I’ve tried:

ExecStart = ''${pkgs.hallo}/bin/hallo'';

and

ExecStart = ''${hallo.hallo}/bin/hallo'';

both give me ‘attribute hallo missing’ errors.

Please could I have some pointers on how to do this properly? I would also really appreciate links to example flakes that package an application + NixOS service module, because I would ideally like to figure out how to not only get this working, but also how to do it idiomatically/cleanly as well :-).

There are README.md files in nix_flake and nix_flake/test_vm with the commands I’m using to build/test; crucially what I’m doing to test my module is:

cd nix_flake/test_vm
nix flake update
nix build .#nixosConfigurations.testvm.config.system.build.vm -j8
./result/bin/run-nixos-vm

Thanks!

nix-foundryvtt is a flake that packages Foundry VTT and provides a service module for NixOS. The way I handle making foundryvtt available to the module is by adding an extra argument to the module for the flake.

# flake.nix
# ...
      nixosModules.foundryvtt = import ./modules/foundryvtt self;
# ...

# modules/foundryvtt/default.nix
flake: { config, lib, pkgs, ... }:
# ...
  inherit (flake.packages.${pkgs.stdenv.hostPlatform.system}) foundryvtt;
# ...
      package = mkOption {
        type = types.package;
        default = foundryvtt;
        description = ''
          The Foundry package to use with the service.
        '';
      };
# ...
        ExecStart = "${cfg.package}/bin/foundryvtt --headless --noupdate --dataPath=\"${dataDir}\"";
# ...

I then access my foundryvtt package via flake.packages.<system>.foundryvtt. You’re encountering an error because pkgs refers to nixpkgs, which does not include your hallo package (it’s in your flake’s packages).

2 Likes

Eureka! Thanks! That (plus a bit of tweaking to my package crate, which somehow failed to build the package when depended on by another flake) works :-).

1 Like

Out of interest, was the issue with a dependant flake basically this In the Nix language — nix.dev documentation?

I’m not sure, but I don’t think so. Unfortunately I don’t really remember very well or understand much; I’ve tried a lot of things to try and get something working :slight_smile: