Is activating a service defined in a Flake via `nix develop` a thing?

I am trying to implement a fully defined and reproducible repo that depends on ollama. ollama depends on it’s service running. I have defined the service in my Flake but nix develop does not start the service. nix flake check does not flag the Flake as being malformed. When I aam reviewing documentation for Flakes, I do not immediately see mention of services.

Here are some of the places I have looked:
https://nix.dev/manual/nix/2.25/command-ref/new-cli/nix3-flake.html

Here is my Flake:

{
  description = "Define development dependencies.";

  inputs = {
    # Which Nix upstream package branch to track
    nixpkgs.url = "nixpkgs/nixos-unstable";
  };

  # What results we're going to expose
  outputs = { self, nixpkgs }:
    let
      # What packages and system functions we'll use
      pkgs = import nixpkgs {  system = "x86_64-linux"; };
      python_name = "python3.12";
      python = pkgs.python312;
    in {
      devShells.x86_64-linux.default = pkgs.mkShell rec {
        packages = with pkgs; [
          python3Full
          (poetry.override { python3 = python; })
          direnv
          gcc-unwrapped
          stdenv
          ruff
          ollama-rocm
          # NOTE: Put additional packages you need in this array. Packages may be found by looking them up in
          # https://search.nixos.org/packages
        ];

        # Getting the library paths needed for Python to be put into
        # LD_LIBRARY_PATH
        pythonldlibpath = "${pkgs.stdenv.cc.cc.lib}/lib:${pkgs.stdenv.cc.cc.lib.outPath}/lib:${pkgs.lib.makeLibraryPath packages}";

        # Run the following on the shell, which builds up LD_LIBRARY_PATH.
        shellHook = ''
        export LD_LIBRARY_PATH="${pythonldlibpath}"
        '';
      };
      services.ollama = {
        enable = true;
        acceleration = "rocm";
        environmentVariables = {
          HCC_AMDGPU_TARGET = "gfx1100";
          HSA_OVERRIDE_GFX_VERSION = "11.0.0";
        };
        rocmOverrideGfx = "11.0.0";
      };
  };
}

I am testing this by simply running ollama run deepseek-r1.

1 Like

If you are going to use service in nix develop which is flakes shell.
You have to look into devenv.sh

I found this one, which uses nix flakes, devenv for basic ollama.

Hope this help in progressing further.
I’m also looking into making a devenv for Ollama looking into nixos service.ollama.enable config code.
It is little difficult for me to understand all the config.

You can create your own config similar to nixos Ollama using devenv.

1 Like

there is also GitHub - juspay/services-flake: NixOS-like services for Nix flakes

1 Like

I’m already using direnv. Do you know if there is any substantial difference between the two off hand?

Direnv activate script for devenv.

Devenv is nix shell with something call process which can act as service.

My sentence might not be right, but this is the general idea.

1 Like

These really emphasized a use of flakes to specify a service and not packages. I’m still new, so it took some time to chew threw. The links helped but did not solve my issue. I did ultimately get something close enough.

{
  description = "Define development dependencies.";

  inputs = {
    nixpkgs.url = "nixpkgs/nixos-unstable";
    process-compose-flake.url = "github:Platonic-Systems/process-compose-flake";
    services-flake.url = "github:juspay/services-flake";
  };

  outputs = { nixpkgs, process-compose-flake, services-flake, ... }:
    let
      supportedSystems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" ];
      forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f rec {
        pkgs = import nixpkgs { inherit system; };

        servicesMod = (import process-compose-flake.lib { inherit pkgs; }).evalModules {
          modules = [
            services-flake.processComposeModules.default
            {
              services.ollama."ollama1" = {
                enable = true;
                acceleration = "rocm";
              };
            }
          ];
        };
      });

    in {
      packages = forAllSystems ({ servicesMod, ... }: {
        default = servicesMod.config.outputs.package;
      });

      devShells = forAllSystems ({pkgs, servicesMod}: {
        default = pkgs.mkShell rec {
          packages = with pkgs; [
            python3Full
            (poetry.override { python3 = pkgs.python312; })
            direnv
            gcc-unwrapped
            stdenv
            ruff
            ollama-rocm
          ];

          pythonldlibpath = "${pkgs.stdenv.cc.cc.lib}/lib:${pkgs.stdenv.cc.cc.lib.outPath}/lib:${pkgs.lib.makeLibraryPath packages}";

          shellHook = ''
              export LD_LIBRARY_PATH="${pythonldlibpath}"
          '';
        };
      });
    };
}

Which is then able to have the service started via nix run .#default.