-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
167 lines (143 loc) · 4.26 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
{
inputs = {
opam-nix.url = "github:tweag/opam-nix";
nixpkgs.follows = "opam-nix/nixpkgs";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs =
{ self
, opam-nix
, nixpkgs
, nixpkgs-unstable
}:
let
package = "avatar-bot";
systems = [
"x86_64-linux"
];
# Packages from devPackagesQuery
getOcamlDevPackages = pkgs: scope:
builtins.attrValues
(pkgs.lib.getAttrs (builtins.attrNames devPackagesQuery) scope);
# Ocaml development packages
devPackagesQuery = {
ocaml-lsp-server = "*";
ocamlformat = "*";
};
# You can force versions of certain packages here
query = devPackagesQuery // {
ocaml-base-compiler = "*";
};
createScope = pkgs: on: on.buildDuneProject { pkgs = pkgs; } package ./. query;
getDevPackages = pkgs:
with pkgs;[
# Nix tools
nil
nixpkgs-fmt
# Cloud provider
google-cloud-sdk
];
overlay = final: prev: {
${package} = prev.${package}.overrideAttrs
(attr: {
# Prevent the ocaml dependencies from leaking into dependent environments
doNixSupport = false;
});
};
forSystems = f:
nixpkgs.lib.genAttrs systems
(system:
let
pkgs = nixpkgs.legacyPackages.${system};
on = opam-nix.lib.${system};
scope = (createScope pkgs on).overrideScope' overlay;
in
f system pkgs scope
);
getDockerImage = pkgs: pkg: pkgs.dockerTools.buildImage {
name = package;
tag = "latest";
created = "now";
copyToRoot = pkgs.buildEnv {
name = "image-root";
paths = [ pkg pkgs.iana-etc ];
pathsToLink = [ "/bin" "/etc" ];
};
extraCommands = ''
mkdir -p ./tmp
'';
config = {
Cmd = [ "/bin/${package}" ];
Env = [ "SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt" ];
ExposedPorts = {
"80/tcp" = { };
};
};
};
in
nixpkgs.lib.recursiveUpdate
rec {
packages = forSystems (_: pkgs: scope:
rec {
default = scope.${package};
docker = getDockerImage pkgs default;
}
);
formatter = forSystems (_: pkgs: _: pkgs.nixpkgs-fmt);
devShells = forSystems (system: pkgs: scope:
{
default =
pkgs.mkShell {
inputsFrom = [ packages.${system}.default ];
buildInputs =
getOcamlDevPackages pkgs scope
++ getDevPackages pkgs;
shellHook = ''
set -a
source .env
set +a
'';
};
}
);
}
(
let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
pkgsMusl = pkgs.pkgsMusl;
on = opam-nix.lib.x86_64-linux;
overlayMusl = final: prev: {
${package} = prev.${package}.overrideAttrs
(attr: {
# Prevent the ocaml dependencies from leaking into dependent environments
doNixSupport = false;
buildInputs = attr.buildInputs ++ [
pkgs.pkgsStatic.gmp
pkgs.pkgsStatic.openssl
pkgs.pkgsStatic.libev
];
buildPhase = "dune build -p avatar-bot --profile static";
}
);
};
scopeMusl =
((createScope pkgsMusl on).overrideScope' overlayMusl);
mainMusl = scopeMusl.${package};
in
rec {
packages.x86_64-linux.static = mainMusl;
packages.x86_64-linux.staticDocker =
getDockerImage pkgs packages.x86_64-linux.static;
devShells.x86_64-linux.static = pkgsMusl.mkShell {
inputsFrom = [ mainMusl ];
buildInputs = getOcamlDevPackages pkgs scopeMusl
++ getDevPackages pkgs;
shellHook = ''
set -a
source .env
set +a
'';
};
}
);
}