Skip to content

Commit 19c576b

Browse files
committed
Implementing Nix binary retrieval
1 parent 54a7116 commit 19c576b

File tree

5 files changed

+103
-27
lines changed

5 files changed

+103
-27
lines changed

src/builder.ab

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Licensed under GNU GPLv2
33
// Functions to build Nix and its dependencies from source
44

5+
import { set_title } from "./term.ab"
56
import { check_deps, bail } from "./common.ab"
67
import { pull_source_file, pull_binary } from "./resources.ab"
7-
import { set_title } from "./term.ab"
88

99
fun build_openssl()
1010
{

src/main.ab

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ let args = [Text]
1717
import { env_var_get } from "std/env"
1818

1919
import { bail, untar, check_deps } from "./common.ab"
20-
import { get_repo_root } from "./platform.ab"
2120
import { catch_args } from "./cli.ab"
2221
import { launch_nix } from "./nix.ab"
2322

src/nix.ab

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,63 @@
22
// Licensed under GNU GPLv2
33
// Final-step Nix invocation methods
44

5-
import { build_nix } from "./builder.ab"
6-
import { pull_binary } from "./resources.ab"
7-
import { enter_alt_buffer, set_title, teardown } from "./term.ab"
5+
import { file_exists, dir_exists, dir_create } from "std/fs"
86

9-
fun is_ignore_system(): Bool
7+
import { enter_alt_buffer, set_title, teardown } from "./term.ab"
8+
import { bail } from "./common.ab"
9+
import { get_osname, get_cache_root, get_system } from "./platform.ab"
10+
import { pull_binary } from "./resources.ab"
11+
import { build_nix } from "./builder.ab"
12+
13+
/// Determine whether Nix is installed system-wide. Nixie can launch the
14+
/// system-wide Nix when it is available, using the script's bundled options.
15+
///
16+
/// This is the predicate for that behavior.
17+
fun is_nix_installed(): Bool
1018
{
11-
return env_var_test("nosystem")
19+
// --nixie-ignore-system flag set
20+
if env_var_test("nosystem"): return false
21+
22+
// Simple enough heuristic. In the future, might also check for the `nix`
23+
// command, but that would create a ternary situation where Nixie's static
24+
// Nix would need to use the system store.
25+
if dir_exists("/nix/store"): return true
26+
27+
return false
1228
}
1329

30+
/// Attempt to retrieve the static Nix binary by all known methods.
31+
///
32+
/// This function will first try to extract or download a static executable
33+
/// for the current system, and if it fails, a local build from source will be
34+
/// attempted.
35+
///
36+
/// **NOTE**: On macOS, this function is also responsible for downloading
37+
/// `libfakedir.dylib`.
1438
fun get_nix()
1539
{
40+
let cache_root = get_cache_root()
41+
let osname = get_osname()
42+
let system = get_system()
43+
44+
let nix_path = "{cache_root}/nix-static"
45+
let fakedir_path = "{cache_root}/nix-lib/libfakedir.dylib"
46+
1647
enter_alt_buffer()
1748
set_title("Building Nix...")
1849

1950
// Cleanly exit alt-buffer if hit with ^C
2051
trust $trap "{nameof teardown}; exit 1" SIGKILL SIGTERM SIGINT SIGABRT$
2152

22-
//TODO: ✨MAGIC✨
53+
if osname == "Darwin" and not file_exists(fakedir_path) {
54+
dir_create("{cache_root}/nix-lib")
55+
trust pull_binary("libfakedir.dylib", fakedir_path)
56+
}
57+
58+
if not file_exists(nix_path) {
59+
trust pull_binary("nix.{system}", nix_path)
60+
}
61+
2362
echo "Pretend we're doing something here..."
2463
trust $sleep 1$
2564

@@ -28,7 +67,12 @@ fun get_nix()
2867
fail 1
2968
}
3069

31-
fun launch_darwin_workaround()
70+
/// Launch Nix with the proper environment variables for fakedir and OpenSSL.
71+
///
72+
/// This is required because macOS has no namespacing facility, and Nix will
73+
/// not attempt to use the system-wide CA bundle by default. This function
74+
/// addresses both issues.
75+
fun launch_darwin_workaround(name: Text, args: [Text]): Null
3276
{
3377

3478
}

src/platform.ab

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,47 @@
33
// Environment-specific data retrieval utilities
44

55
import { env_var_get, echo_warning } from "std/env"
6-
import { split, join } from "std/text"
7-
import { array_pop } from "std/array"
6+
import { split, join } from "std/text"
7+
import { array_pop } from "std/array"
88

99
import { get_self } from "./common.ab"
1010

11-
pub fun get_osname(): Text {
11+
/// Return the current operating system, as reported by `uname -s`.
12+
pub fun get_osname(): Text
13+
{
1214
return trust $uname -s$
1315
}
1416

15-
pub fun get_machine(): Text {
16-
return trust $uanme -m$
17+
/// Return the current machine architecture, as reported by `uname -s`.
18+
pub fun get_machine(): Text
19+
{
20+
return trust $uname -m$
1721
}
1822

19-
pub fun get_system(): Text {
23+
/// Return the combined system-string, in the format `osname.machine`.
24+
///
25+
/// See [`get_osname()`](#get_osname) and [`get_machine()`](#get_machine) above.
26+
pub fun get_system(): Text
27+
{
2028
let osname = get_osname()
2129
let machine = get_machine()
2230

2331
return "{osname}.{machine}"
2432
}
2533

26-
pub fun get_dll_ext(): Text {
34+
/// Return the extension for dynamic libraries on the current system.
35+
pub fun get_dll_ext(): Text
36+
{
2737
let osname = get_osname()
2838
if {
2939
osname == "Darwin": return "dylib"
3040
else: return "so"
3141
}
3242
}
3343

34-
pub fun get_nix_root(): Text {
44+
/// Return the path to the user-mode root for the Nix store.
45+
pub fun get_nix_root(): Text
46+
{
3547
let userhome = trust env_var_get("HOME")
3648
let osname = get_osname()
3749
if {
@@ -40,7 +52,9 @@ pub fun get_nix_root(): Text {
4052
}
4153
}
4254

43-
pub fun get_cache_root(): Text {
55+
/// Return the path to the user's cache directory on the current system.
56+
pub fun get_cache_root(): Text
57+
{
4458
let userhome = trust env_var_get("HOME")
4559
let osname = get_osname()
4660
if {
@@ -49,12 +63,16 @@ pub fun get_cache_root(): Text {
4963
}
5064
}
5165

66+
/// Return the path to the root of the Git repository the script is located in.
67+
///
68+
/// Falls back to returning the script's parent directory if not in a repository,
69+
/// or if `git` is unavailable.
5270
pub fun get_repo_root(): Text
5371
{
5472
let self_a = split(get_self(), "/")
5573
array_pop(self_a)
5674

57-
let self_dir = "/{join(self_a, "/")}"
75+
let self_dir = "/" + join(self_a, "/")
5876

5977
return $git -C {self_dir} rev-parse --show-toplevel$ failed {
6078
echo_warning("Failed to find current Git repository, using script parent directory.")

src/resources.ab

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,51 @@
33
// Resource retrieval from tarball or Cachix
44

55
import { file_download } from "std/http"
6+
import { env_var_get } from "std/env"
67

78
import { untar } from "./common.ab"
89

910
fun cachix_url(derivation: Text, member: Text): Text
1011
{
12+
let SOURCE_CACHE = trust env_var_get("SOURCE_CACHE")
13+
1114
return "https://{SOURCE_CACHE}/serve/{derivation}/{member}"
1215
}
1316

1417
pub fun pull_source_file(member: Text, dest: Text): Null?
1518
{
16-
let where = untar("sources/{member}") failed {
19+
let SOURCE_DERIVATION = trust env_var_get("SOURCE_DERIVATION")
20+
21+
let where = trust untar("sources/{member}")
22+
if status != 0 {
1723
let tmpf = trust $mktemp -t nixie_src_XXXXXXXX.tgz$
1824
let tmpd = trust $mktemp -t -d nixie_{member}_XXXXXXXX$
1925

20-
file_download(cachix_url(SOURCE_DERIVATION, "{member}.tar.gz"), tmpf)?
26+
if not file_download(cachix_url(SOURCE_DERIVATION, "{member}.tar.gz"), tmpf) {
27+
fail 1
28+
}
29+
2130
trust $rm -f {tmpf}$
22-
return tmpd
31+
where = tmpd
2332
}
2433

25-
mv where dest
34+
trust mv where dest
2635
}
2736

2837
pub fun pull_binary(member: Text, dest: Text): Null?
2938
{
30-
let where = untar(member) failed {
39+
let NIX_BINS_DERIVATION = trust env_var_get("NIX_BINS_DERIVATION")
40+
41+
let where = trust untar(member)
42+
if status != 0 {
3143
let tmpf = trust $mktemp -t nixie_{member}_XXXXXXXX$
3244

33-
file_download(cachix_url(BINARIES_DERIVATION, member), tmpf)?
34-
return tmpf
45+
if not file_download(cachix_url(NIX_BINS_DERIVATION, member), tmpf) {
46+
fail 1
47+
}
48+
49+
where = tmpf
3550
}
3651

37-
mv where dest
52+
trust mv where dest
3853
}

0 commit comments

Comments
 (0)