Well, to answer the question, you need to give it the full path to nix with something like ExecStart = "${pkgs.nix}/bin/nix ......"; But this probably isn’t how you want to do this.
You should most likely be taking that other flake as an input to your own flake and referring to its outputs directly, instead of using nix run in a the service. i.e. In flake.nix you can have
and then you can use something like ExecStart = "${mygitrepo.apps.${pkgs.system}.program}"; where mygitrepo is the argument passed to your flake’s outputs function. Adjust for whatever that other repo’s outputs actually are.
Even though you received answers to your question, please let me also say, that using nix run in the service is not the way you should do this.
It introduces a dependency on the network being online
It updates the service at random
Starting the service might get delayed for necessary builds
To mitigate those problems the proper way was to add the remote flake as an input to your flake and then use their packages in the service to start their program.
If that doesn’t do it, what error do you get? If it’s the IFD one you’d either need to significantly rewrite the flake, *or" allow IFD - didn’t realize that was already made an error.
If that doesn’t do it, what error do you get? If it’s the IFD one you’d either need to significantly rewrite the flake, *or" allow IFD - didn’t realize that was already made an error.
Ah of course, it should be referenced as a variable.
So I realized that I needed to put the entire systemd part into a flake,
and wrote the following
You do not. You can create a nixosModule if you want to, but this is not necessary. Just put what you have there in your NixOS configuration and it will work fine.
If you want to create a nixosModule, you’d put it in the same flake as the package, and you would do something like this:
# /projects/myApp/flake.nix
{
outputs = {self, ...}: {
packages.${system}.default = { <some package definition> };
nixosModules.default =
# For illustration, probably want to break this definition out to a separate file
{ config, pkgs, lib, ... }: {
options = {
services.myApp.enable = lib.mkEnableOption "myApp";
};
config = lib.mkIf config.services.myApp.enable {
systemd.services.myApp = {
# Insert systemd config here
serviceConfig.ExecStart = "${self.packages.${pkgs.system}.default}/bin/main";
};
};
};
};
}
Then you can import that from your other flake:
# /etc/nixos/flake.nix
{
inputs = {
myApp.url = "git+https://myAppUrl.com";
};
outputs = { myApp, ... } @ inputs: {
# You may want to use a name other than "nix" for your hostname, by the way
nixosConfigurations.nix = nixpkgs.lib.nixosSystem {
system = "x86_64-linux"; # Probably
specialArgs = {
inherit inputs;
};
modules = [
./configuration.nix
];
};
};
}
[nix-shell:/etc/nixos]# nixos-rebuild switch --flake /etc/nixos --show-trace
error: failed to extract archive (Can't create '/run/user/0/nix-148004-0/NixOS-nixpkgs-b5680f5/pkgs/development/tools/wasmi/default.nix')
… while fetching the input 'github:NixOS/nixpkgs'
… while updating the flake input 'nixpkgs'
… while updating the lock file of flake 'path:/etc/nixos?lastModified=1705593106&narHash=sha256-JK7oxdDQXLQaDzLBhPzejVoJPz9%2fyIW6Ogw1KBwC2oU='
/etc/nixos is owned by root, I assume, and you’re executing this as a normal user? If so, you can’t write to the flake.lock that’s automatically created and filled with entries for all your inputs if you don’t have one yet.
I’d recommend moving your config to a directory owned by your user, and running:
Argh, such a simple error. Yes, I was not root as I had ran nix-shell -p vim, I thought I would still remain in root after that.
Running it now, I get the error
error: flake 'path:/etc/nixos' does not provide attribute 'packages.x86_64-linux.nixosConfigurations."nix".config.system.build.nixos-rebuild', 'legacyPackages.x86_64-linux.nixosConfigurations."nix".config.system.build.nixos-rebuild' or 'nixosConfigurations."nix".config.system.build.nixos-rebuild'
Not sure if I’ve missed something after following the tutorial of the
blog post you linked?
Not quite, you will also need to run (in /etc/nixos, or wherever your system config flake lives):
nix flake lock --update-input myApp
or, to update all your inputs (including nixpkgs):
nix flake update
This will update the contents in flake.lock to point to the newest commits for the branches of each of your inputs, which will then be used by nixos-rebuild switch to build your configuration. If you don’t update flake.lock, nix will just build the same commit over and over again.
Updates are deliberately explicit, you don’t want stuff just changing from underneath your feet with a tool that advertises reproducibility.