# How to build a docker image with a working Nix inside it

**URL:** https://discourse.nixos.org/t/how-to-build-a-docker-image-with-a-working-nix-inside-it/32960
**Category:** Help
**Created:** [September 12, 2023, 3:44pm UTC](https://discourse.nixos.org/t/how-to-build-a-docker-image-with-a-working-nix-inside-it/32960 "2023-09-12T15:44:56Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![wamserma](https://discourse.nixos.org/user_avatar/discourse.nixos.org/wamserma/32/5353_2.png) [@wamserma](https://discourse.nixos.org/u/wamserma)
#### Post date: [September 13, 2023, 6:32am UTC](https://discourse.nixos.org/t/how-to-build-a-docker-image-with-a-working-nix-inside-it/32960/2 "2023-09-13T06:32:19Z")

</div>

You can simply pick the code that builds the official nix container and base your own container on it. I have a flake that contains (partly redacted):

```auto
inputs = {
   nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
   flake-utils.url = "github:numtide/flake-utils";
   nix = {
     url = "github:NixOS/nix";
     inputs.nixpkgs.follows = "nixpkgs";
   };
  };

outputs = { self, nix, nixpkgs, flake-utils }:

    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs { inherit system; };
        flakeRegistry = {
          nixos = {
            from = { type = "indirect"; id = "nixos"; };
            to = nixpkgs;
            exact = true;
          };
          nixpkgs = {
            from = { type = "indirect"; id = "nixpkgs"; };
            to = nixpkgs;
            exact = true;
          };
        };

        flakeRegistryJSON = (pkgs.formats.json { }).generate "flake-registry.json" {
          version = 2;
          flakes =
            pkgs.lib.mapAttrsToList (n: v: { inherit (v) from to exact; }) flakeRegistry;
        };

        containerInfo = {
          name = "mycontaner";
          tag = nixpkgs.rev;
        };
        flake-input-sources = pkgs.writeTextFile rec { name = "flake-inputs.log"; text = ''${nixpkgs.outPath} ${nix.outPath}''; destination = "/${name}"; };
        # use official nix container as base to get everything for flake-based building
        container-layered = pkgs.lib.throwIfNot (pkgs.stdenv.isLinux) "Docker images are only supported on Linux." (import ("${nix.outPath}" + "/docker.nix") {
          inherit (containerInfo) name tag;
          inherit pkgs;
          bundleNixpkgs=false;
          extraPkgs = [pkgs.git pkgs.which pkgs.stdenvNoCC] ++ [flake-input-sources];
          maxLayers = 111;
          nixConf = {
            experimental-features = ["nix-command" "flakes"];
          };
          flake-registry = flakeRegistryJSON;
        });

      in
      rec {
        devShell = pkgs.mkShell {
          buildInputs = [run-packages dev-packages];
          shellHook = '' echo Hello '';
        };
        packages = flake-utils.lib.flattenTree {
          container = container-layered;
        };

```

to create a flake-enabled Docker image that I use in CI.

---

_[View the full topic](https://discourse.nixos.org/t/how-to-build-a-docker-image-with-a-working-nix-inside-it/32960)._
