Motivation: I wanted to migrate an old project to my newly set-up web server running NixOS. I already was using Docker and therefore I thought it easier to keep using a container approach and replicate the Dockerfile with the dockerTools from nixpkgs. (Later down the road I want to look into how to build and run node/deno projects without the need of oci-containers). I started with a simple example to get the foundational understanding how it works, but I couldn’t get a detail working and have some questions about best practices:
# file structure
- /flake.nix
- /src/
- /src/index.html
# flake.nix
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
utils.url = "github:numtide/flake-utils";
};
outputs =
{ nixpkgs, utils, ... }:
utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
};
in
{
packages.default = pkgs.dockerTools.buildLayeredImage {
name = "deno-nix-demo";
tag = "0.0.1";
contents = [
pkgs.coreutils
pkgs.bash
(pkgs.buildEnv {
name = "copy";
paths = [ ./. ];
pathsToLink = [ "/src" ];
})
];
extraCommands = ''
mkdir -p ./tmp/deno
'';
config = {
Env = [
"DENO_DIR=/tmp/deno"
];
WorkingDir = "/app";
Cmd = [
"${pkgs.deno}/bin/deno"
"run"
"--allow-net"
"--allow-read"
"jsr:@std/http/file-server"
];
};
};
}
);
}
The thing I could not figure out: How can I copy the contents of the local directory “/src/” inside the image to a folder with different name (in my example “/app/”)? I tried it with pkgs.buildEnv
, lib.fileset.toSource
.
I also tried both buildLayeredImage
and buildImage
as I assume they would both be able to help me achieve my goal. But I don’t really understand the difference between the contents
attribute within buildLayeredImage
and copyToRoot within buildImage
. Does there exist any good explanation concerning those?
Deno (in my example) seems to require an existing folder (set via DENO_DIR
). Is my approach ok or are there better approaches to handle this? And does it matter if I used extraCommands
or fakeRootCommands
?
I have used Nix now for a bit over a year mainly for dev shells (and I love it for it). I just started using it for my web server and would be very thankful for any kind of helpful input and pointers to explanation to better understand the problem and possible solutions.
Thank you in advance