-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflake.nix
122 lines (108 loc) · 4.34 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
{
inputs.nixpkgs.url = "nixpkgs/nixos-21.11";
inputs.flake-utils.url = "github:numtide/flake-utils";
description =
"Screed is a minimal frontend for the foundation mining-pool software suite.";
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
screed_overlay = (final: prev: { inherit screed; });
pkgs = import nixpkgs {
inherit system;
overlays = [ screed_overlay ];
};
inherit (pkgs)
stdenv callPackage nodejs-16_x nodePackages writeShellScriptBin
dockerTools;
nodejs = nodejs-16_x;
## Import & invoke the generated files from node2nix
#
# To update generated files after a dep. change / update:
# Re-generate package-lock.json after updates using 'npm i'.
# Then run node2nix again either manually, as detailed in
# writeShellScriptBin below, or use the devShell which includes it.
generated = callPackage ./nix { inherit nodejs; };
name = "screed";
screed = {
## Build recipe for the static assets manually instead of calling
# the generated.package (which uses buildNodePackage internally
# and expects to install binaries..etc)
static = stdenv.mkDerivation {
inherit name;
src = ./.;
buildInputs = [ nodejs ];
buildPhase = ''
ln -s ${generated.nodeDependencies}/lib/node_modules ./node_modules
export PATH="${generated.nodeDependencies}/bin:$PATH"
npm run build
'';
installPhase = ''
cp -r build $out/
'';
};
## Tarball for releases
tarball = generated.tarball.override { buildInputs = [ nodejs ]; };
## Docker image
# nix build '.#image'
# The resulting image can be loaded directly:
# docker load < result
# And executed:
# docker run -d --rm -p 5000:5000 screed
image = dockerTools.buildLayeredImage {
inherit name;
tag = "latest";
# The closure of config is automatically included in the closure of
# the final image, so no need to explicitly set contents property.
config = {
Cmd = [
"${generated.nodeDependencies}/bin/sirv" "${screed.static}"
"--no-clear"
"--host" "0.0.0.0"
];
ExposedPorts = { "5000/tcp" = { }; };
};
};
# Launch development server
dev = writeShellScriptBin "dev" ''
rm -rf ./node_modules
ln -s ${generated.nodeDependencies}/lib/node_modules ./node_modules
export PATH="${generated.nodeDependencies}/bin:$PATH"
nix develop --command npm start
'';
};
# node2nix wrapper to update nix files on npm changes
node2nix = writeShellScriptBin "node2nix" ''
${nodePackages.node2nix}/bin/node2nix \
--development \
-l package-lock.json \
-c ./nix/default.nix \
-o ./nix/node-packages.nix \
-e ./nix/node-env.nix
'';
in rec {
overlays = pkgs.overlays;
packages = {
static = pkgs.screed.static;
image = pkgs.screed.image;
tarball = pkgs.screed.tarball;
};
# 'nix build' to build default package
defaultPackage = packages.static;
# 'nix run' to symlink node_modules & launch development server:
apps.screed = flake-utils.lib.mkApp { drv = pkgs.screed.dev; };
defaultApp = apps.screed;
# Development shell with node2nix wrapper script
# TODO add javascript + typescript language server
# Runs when 'nix develop' is called:
# 'nix develop' to start dev env without symlinking or running anything:
devShell = generated.shell.override { buildInputs = [ node2nix nodejs ]; };
## TODO: add nixosModule;
# Goal:
# | services.screed.enable = true;
# | services.screed.domain = "example.com";
# nixosModules.screed = { pkgs, ... }: {
# nixpkgs.overlays = pkgs.overlays;
# systemd.services = {};
# };
});
}