cachix / devenv

Fast, Declarative, Reproducible, and Composable Developer Environments
https://devenv.sh
Apache License 2.0
4.13k stars 311 forks source link

Postgres service doesn't start using the flake.parts template #1299

Closed glottologist closed 2 months ago

glottologist commented 3 months ago

Describe the bug Postgres service doesn't start using the flake.parts template.

To reproduce Run nix develop --impure with the following flake:

{
  description = "Description for the project";

  inputs = {
    devenv-root = {
      url = "file+file:///dev/null";
      flake = false;
    };
    nixpkgs.url = "github:cachix/devenv-nixpkgs/rolling";
    devenv.url = "github:cachix/devenv";
    nix2container.url = "github:nlewo/nix2container";
    nix2container.inputs.nixpkgs.follows = "nixpkgs";
    mk-shell-bin.url = "github:rrbutani/nix-mk-shell-bin";
  };

  nixConfig = {
    extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
    extra-substituters = "https://devenv.cachix.org";
  };

  outputs = inputs @ {
    flake-parts,
    devenv-root,
    ...
  }:
    flake-parts.lib.mkFlake {inherit inputs;} {
      imports = [
        inputs.devenv.flakeModule
      ];
      systems = ["x86_64-linux" "i686-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin"];

      perSystem = {
        config,
        self',
        inputs',
        pkgs,
        system,
        ...
      }: {
        # Per-system attributes can be defined here. The self' and inputs'
        # module parameters provide easy access to attributes of the same
        # system.

        # Equivalent to  inputs'.nixpkgs.legacyPackages.hello;
        packages.default = pkgs.hello;

        devenv.shells.default = {
          devenv.root = let
            devenvRootFileContent = builtins.readFile devenv-root.outPath;
          in
            pkgs.lib.mkIf (devenvRootFileContent != "") devenvRootFileContent;

          name = "my-project";

          imports = [
            # This is just like the imports in devenv.nix.
            # See https://devenv.sh/guides/using-with-flake-parts/#import-a-devenv-module
            # ./devenv-foo.nix
          ];

          # https://devenv.sh/reference/options/
          packages = [config.packages.default];

          enterShell = ''
            hello
          '';

          processes.hello.exec = "hello";

          services.postgres = {
            enable = true;
            initialDatabases = [{name = "postgres";}];
          };
        };
      };
      flake = {
        # The usual flake attributes can be defined here, including system-
        # agnostic ones like nixosModule and system-enumerating ones, although
        # those are more easily expressed in perSystem.
      };
    };
}

I would expect a postgres instance to be spun up according to the service specification.

Version Using flakes ver 1.0.7

sandydoo commented 3 months ago

Postgres service doesn't start using the flake.parts template.

Is there an error? What do you get when you run devenv up?

glottologist commented 3 months ago

@sandydoo No error. The shell comes up and uses the hello package but there is no instance of Postgres, nor any reference to Postgres when the shell is building as far as I can tell

glottologist commented 3 months ago

@sandydoo I also don't think this is specific to Postgres as if I add other services like:


          services.mysql.enable = true;
          services.nginx.enable = true;
          services.influxdb.enable = true;
          services.couchdb.enable = true;
          services.cassandra.enable = true;
          services.clickhouse.enable = true;

I can see the packages being built in the nix store but the services do not come up in the dev env

sandydoo commented 3 months ago

I cannot replicate this on my end. The following works as expected:

nix develop --impure
devenv up

The processes need to be launched with devenv up. They're not launched when entering the shell.

Eveeifyeve commented 2 months ago

This issue is happening to me. The problem is devenv is not creating state in .devenv/state/postgres and says that it has a permission error.

Repoduce

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    devenv.url = "github:cachix/devenv";
    nix2container.url = "github:nlewo/nix2container";
    nix2container.inputs.nixpkgs.follows = "nixpkgs";
    fenix = {
      url = "github:nix-community/fenix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

    nixConfig = {
    extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
    extra-substituters = "https://devenv.cachix.org";
  };

  outputs =
    inputs@{ flake-parts, nixpkgs, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      imports = [ inputs.devenv.flakeModule ];
      systems = nixpkgs.lib.systems.flakeExposed;
      perSystem =
        {
          config,
          self',
          inputs',
          lib,
          pkgs,
          system,
          ...
        }:
        {
          devenv.shells.default = {
            difftastic.enable = true;
            packages = lib.optionals pkgs.stdenv.isDarwin (
              with pkgs;
              [
                darwin.apple_sdk.frameworks.Security
                darwin.apple_sdk.frameworks.SystemConfiguration
                sqlx-cli
                bacon
              ]
            );
            languages.rust = {
              enable = true;
              channel = "stable"; # or "nightly"
              toolchain = {
                rustc = pkgs.rustc-wasm32;
              };
              targets = [ "wasm32-unknown-unknown" ];
            };

            services.postgres = {
              enable = true;
              package = pkgs.postgresql_16; 
              listen_addresses = "127.0.0.1";
              initialDatabases = [{ name = "teaclient"; }];
            };

            env = {
              DATABASE_URL= "postgresql://teaclient:teaclient123@localhost:6500/teaclient?schema=public";
            };
            dotenv.enable = false;
          };
        };
    };
}

System info

using flake on v1.0.7

Eveeifyeve commented 2 months ago

It works with devenv up.

Eveeifyeve commented 2 months ago

It seems devenv up creates the necessary files to run postgres but nix develop --impure doesn't. if you do mkdir .devenv/state/postgres and then get out and enter it, it will say:

postgres: could not access the server configuration file "/Users/eveeifyeve/projects/TeaClient/API/.devenv/state/postgres/postgresql.conf": No such file or directory

but when you create the file it says this:

2024-07-07 01:21:43.075 GMT [29378] FATAL:  data directory "/Users/eveeifyeve/projects/TeaClient/API/.devenv/state/postgres" has invalid permissions

which could be the issue that devenv has a permission issue with nix develop --impure which means that it can't create the directory. but the weird thing it creates the .devenv/state/run/postgres directory with nothing.

sandydoo commented 2 months ago

It works with devenv up.

@Eveeifyeve, this is how it is intended to work. Services are launched via devenv up.

It seems devenv up creates the necessary files to run postgres but nix develop --impure doesn't.

Initial database setup currently happens when the service is launched (devenv up). What exactly are you trying to accomplish? If you have a compelling use-case, please, open a separate issue.

which could be the issue that devenv has a permission issue with nix develop --impure which means that it can't create the directory. but the weird thing it creates the .devenv/state/run/postgres directory with nothing.

The errors you're seeing have nothing to do with devenv. First of all, everything in .devenv is internal devenv state. You shouldn't be creating or modifying files in there. Secondly, a postgresql database needs to be initialized through initdb. devenv does this and more for you.


Closing this issue as the OP likely needed to launch the services with devenv up. @glottologist, feel free to reopen if that is not the case.