Running flake commands in a nixos server

I’m currently running issues in deploying an app on a NixOS server.

What I’m trying to do is have a NixOS server running that pulls in a website repo that has a flake that specifies how to run the site.

But no matter how I try to make it work, instead of running the commands in the folder where the website code resides, it pulls it into the Nix store and then cannot find the flake file.

The repo with the Nix code that specifies systemd services to run can be found here:

This repo contains my website code, since I couldn’t get submodules to work, and the website also pulls in another Haskell utility with a flake to run it: here

Are there examples, or is there a known way to incorporate and run flake outputs in a NixOS system? I don’t necessarily want to add all the apps on the server as inputs to the flake.nix, but will if I have to.

This is one of the most common confusions around flakes. To evaluate your flake, nix will first copy the directory (either $PWD or the root of your git repo, including only files that have been git add-ed) into the nix store. There is no way around this, and your directory must have a flake.nix.

Why are you trying to make nix not do that? I don’t fully understand your use case.

The way to do that is to add the flake as an input to the system flake, and to use the outputs as usual, indeed. I’m not sure what you’re envisioning otherwise, what would you like it to be?

2 Likes

I am unsure what exactly you want but do remember that you can just nix run github:org/repo#program to run a flake output from github. hope that helps.

1 Like

Thanks @TLATER and @eldritch_cookie for your help, and sorry I cannot formulate my problem more clearly!

What I’m trying to do is declaratively specify a NixOS server using a flake to pull in a configuration.nix file.
I want this server to run and update a blog, reacting to new posts and regenerating the blog outline.
But since flakes pull everything into the store, I am unable to correctly write the systemd services that run the blog commands. The sandbox has no utilities, so I cannot even do a nix run on my blog flake.

Is managing a server that runs and updates apps something better suited to NixOps?

I hope that makes more sense.

ok, couldn’t you use autoUpgrade.flake and specify in autoUpgrade.flags [“–update-input” “myflake” “–commit-lock-file”] and then in your nixos server just specify your server normally. the problem to this approach is that you need to update the flake but i do hope that is easier

Thanks again everyone who helped offer suggestions above!

I managed to get what I wanted working by adding a path option to the systemd service. Documentation

My NixOS server now correctly cds to the target location and run the flake commands I expect for a persistent server.

  systemd.services = {
    cutesealfanpage-hakyll-site = {
      enable = true;
      description = "The hakyll executable that rebuilds the site when a new blog post is created.";
      path = with pkgs; [
        nix
        git
      ];
      script = ''
        cd ${PROJECT_ROOT}
        nix run .#hakyll-site -- watch --no-server
      '';
    };

    cutesealfanpage-generatePosts = {
      enable = true;
      description = "The haskell script that creates the new post of the day.";
      startAt = "08:12:42";
      path = with pkgs; [
        nix
        git
      ];
      script = ''
        cd ${PROJECT_ROOT}
        nix run .#generateSealPosts
      '';
    };