Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
140 lines (128 loc) · 4.77 KB
/
Copy pathflake.nix
File metadata and controls
140 lines (128 loc) · 4.77 KB
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
{
description = "Chat On Steroids development environment";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs =
{ nixpkgs, ... }:
let
package = builtins.fromJSON (builtins.readFile ./package.json);
versionMajor = value:
let
match = builtins.match "[^0-9]*([0-9]+).*" value;
in
if match == null then
throw "Could not read a major version from ${value}"
else
builtins.elemAt match 0;
electronAttr = "electron_${versionMajor package.devDependencies.electron}";
systems = [
"x86_64-linux"
"aarch64-linux"
];
forAllSystems = nixpkgs.lib.genAttrs systems;
developmentFor =
system:
let
pkgs = import nixpkgs { inherit system; };
electron =
if builtins.hasAttr electronAttr pkgs then
pkgs.${electronAttr}
else
throw "nixpkgs does not provide ${electronAttr}, required by package.json";
# Match the development runtime used by CI. @types/node follows
# Electron's embedded Node version, not the host development runtime.
nodejs = pkgs.nodejs_22;
nodeModules = pkgs.importNpmLock.buildNodeModules {
npmRoot = ./.;
inherit nodejs;
derivationArgs = {
# Electron 44's npm install script downloads its own runtime even when
# ELECTRON_SKIP_BINARY_DOWNLOAD is set. Keep dependency installation
# offline/reproducible and point the JS package at nixpkgs Electron below.
npmRebuildFlags = [ "--ignore-scripts" ];
nativeBuildInputs = [ pkgs.pkg-config ];
buildInputs = [ pkgs.vips ];
env.SHARP_FORCE_GLOBAL_LIBVIPS = "1";
preInstall = ''
export PATH="$PWD/node_modules/.bin:$PATH"
# Keep both Electron consumers on the nixpkgs runtime. The npm
# package resolves through path.txt, while electron-vite directly
# spawns node_modules/electron/dist/electron.
printf '%s' electron > node_modules/electron/path.txt
rm -rf node_modules/electron/dist
ln -s ${electron.dist} node_modules/electron/dist
# npm scripts are disabled above, so build the one native dependency
# that intentionally uses nixpkgs libvips instead of a bundled copy.
pushd node_modules/sharp >/dev/null
node install/build.js
popd >/dev/null
'';
};
};
in
{
inherit pkgs electron nodejs nodeModules;
};
in
{
devShells = forAllSystems (
system:
let
development = developmentFor system;
inherit (development) pkgs electron nodejs nodeModules;
inherit (pkgs) lib;
in
{
default = pkgs.mkShell {
packages = [
pkgs.importNpmLock.hooks.linkNodeModulesHook
nodejs
nodejs.passthru.python
electron
pkgs.git
pkgs.gnutar
pkgs.pkg-config
pkgs.vips
pkgs.ripgrep
pkgs.unzip
];
npmDeps = nodeModules;
ELECTRON_OVERRIDE_DIST_PATH = "${electron.dist}";
SHARP_FORCE_GLOBAL_LIBVIPS = "1";
LD_LIBRARY_PATH = lib.makeLibraryPath [
pkgs.stdenv.cc.cc.lib
pkgs.vips
];
};
}
);
checks = forAllSystems (
system:
let
inherit (developmentFor system) pkgs nodejs nodeModules;
in
{
node-modules = nodeModules;
native-runtime = pkgs.runCommand "chat-on-steroids-native-runtime" {
nativeBuildInputs = [ nodejs ];
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [ pkgs.stdenv.cc.cc.lib pkgs.vips ];
} ''
cd ${nodeModules}
node - <<'JS'
const assert = require('node:assert/strict');
const fs = require('node:fs');
const sharp = require('sharp');
const Parser = require('tree-sitter');
const parser = new Parser();
parser.setLanguage(require('tree-sitter-bash'));
assert.equal(parser.parse('echo ready').rootNode.hasError, false);
assert.equal(typeof require('node-pty').spawn, 'function');
assert.ok(fs.existsSync(require('electron')));
sharp({ create: { width: 1, height: 1, channels: 4, background: '#000' } })
.png().toBuffer().then(bytes => assert.ok(bytes.length > 0));
JS
touch $out
'';
}
);
};
}