Reproducable build and dev environments with Nix
1{
2 description = "A Nix-flake-based C/C++ development environment";
3
4 inputs = {
5 nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05";
6 flake-utils.url = "github:numtide/flake-utils";
7 };
8
9 outputs =
10 {
11 nixpkgs,
12 flake-utils,
13 ...
14 }:
15 flake-utils.lib.eachDefaultSystem (
16 system:
17 let
18 pkgs = import nixpkgs {
19 inherit system;
20 config.allowUnfree = true;
21 };
22
23 # Here are all the packages we would like available
24 devDeps = with pkgs; [
25 clang
26 cmake
27 codespell
28 conan
29 cppcheck
30 doxygen
31 gtest
32 lcov
33 vcpkg
34 ];
35
36 # Additional packages for runtime use in the container
37 containerDeps = with pkgs; [
38 bashInteractive
39 coreutils
40 findutils
41 ];
42
43 hook = ''
44 # This is where I'll add any additional auth needed
45 echo "DevShell up!"
46 '';
47 in
48 {
49 # Dev shell
50 devShells.default = pkgs.mkShell {
51 buildInputs = devDeps;
52 shellHook = hook;
53 };
54
55 # Container image
56 packages.devContainer = pkgs.dockerTools.buildImage {
57 name = "cpp-dev";
58 tag = "latest";
59
60 copyToRoot = pkgs.buildEnv {
61 name = "image-root";
62 paths = devDeps ++ containerDeps;
63 };
64
65 extraCommands = ''
66 mkdir -p ./tmp
67 '';
68
69 config = {
70 Cmd = [ "bash" ];
71 Env = [
72 "PATH=${pkgs.lib.makeBinPath devDeps}:${pkgs.lib.makeBinPath containerDeps}:$PATH"
73 ];
74 WorkingDir = "/workspace";
75 };
76 };
77
78 # Nix package build instructions
79 packages.default = pkgs.stdenv.mkDerivation {
80 pname = "Hello";
81 version = "0.1";
82
83 src = ./.;
84
85 nativeBuildInputs = [ pkgs.clang ];
86
87 buildPhase = ''
88 clang main.c
89 '';
90
91 installPhase = ''
92 mkdir -p $out/bin
93 cp a.out $out/bin/hello
94 '';
95 };
96 }
97 );
98}