Build Docker image from devenv flake

Hello,

I use devenv with flake support (Using With Flakes - devenv) for my coding projects. Now I have a project that I’m trying to bundle as a Docker image and I’m not sure how to go about it. Here is the flake defintion:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    systems.url = "github:nix-systems/default";
    devenv.url = "github:cachix/devenv/v0.6.3";
  };

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

  outputs = {
    self,
    nixpkgs,
    devenv,
    systems,
    ...
  } @ inputs: let
    forEachSystem = nixpkgs.lib.genAttrs (import systems);
  in {
    devShells =
      forEachSystem
      (system: let
        pkgs = nixpkgs.legacyPackages.${system};
      in {
        default = devenv.lib.mkShell {
          inherit inputs pkgs;
          modules = [
            {
              packages = [pkgs.rubyPackages_3_2.sass];

              languages.elm.enable = true;
              languages.javascript = {
                enable = true;
              };

              languages.ruby = {
                enable = true;
                package = pkgs.ruby_3_2;
              };

              processes = {
                rails.exec = "server/bin/rails s";
                elm.exec = "cd client && npx elm-land server";
              };
            }
          ];
        };
      });
  };
}

I want to build a Docker image that includes the required dependencies and starts the processes when run. I’m not sure how to go about this, though. Is there a best practice? I think I need to add a package output to the flake file, so something like this:

packages =
      forEachSystem
      (system: let
        pkgs = nixpkgs.legacyPackages.${system};
      in {
        default = pkgs.dockerTools.buildLayeredImage {
          name = "myproject";
          contents = [
            pkgs.ruby_3_2
            pkgs.rubyPackages_3_2.sass
          ];
        };
      });

Am I on the right path with this? I was wondering if there is a way to re-use the devShells definition, so I don’t need to repeat all the dependencies and definitions?

I also know that devenv has support for building containers with the devenv container command, but unfortunately that doesn’t seem to work when using a flake.

1 Like