Skip to content

Instantly share code, notes, and snippets.

@pedromfedricci
Last active September 17, 2022 04:25
Show Gist options
  • Save pedromfedricci/cacfda1b8d8c4c52c707ae40da1865e0 to your computer and use it in GitHub Desktop.
Save pedromfedricci/cacfda1b8d8c4c52c707ae40da1865e0 to your computer and use it in GitHub Desktop.
Nix profile script for nushell
#!/usr/bin/env nu
module nix-profile {
# Return user's `.nix-profile` path, if it exists, `$nothing` otherwise.
def nix-profile [] {
if ("HOME" in (env).name && "USER" in (env).name) {
$"($env.HOME)/.nix-profile"
}
}
# Return `true` if user's `.nix-profile` path exists, `false` otherwise.
def nix-profile? [] {
not (nix-profile | empty?)
}
# Prepend path to source, removing it's duplicates. If source is not a `string`
# or `list<string>`, then return the original value.
def nix-path [path: path] {
let input = $in
let type = ($input | describe)
if ($type == "string") {
let entry = $"($path):"
build-string $entry ($input | str replace $entry "" --all)
} else if ($type == "list<string>") {
$input | where -b { |it| $it != $path } | prepend $path
} else {
$input
}
}
# Set `$env.NIX_SSL_CERT_FILE` so that Nixpkgs applications like curl will work.
# If no cert file is found, then set it to `$nothing`.
export env NIX_SSL_CERT_FILE {
# System path candidates for `$env.NIX_SSL_CERT_FILE`.
let system_paths = [
# NixOS, Ubuntu, Debian, Gentoo, Arch.
"/etc/ssl/certs/ca-certificates.crt",
# openSUSE Tumbleweed.
"/etc/ssl/ca-bundle.pem",
# Old NixOS.
"/etc/ssl/certs/ca-bundle.crt",
# Fedora, CentOS.
"/etc/pki/tls/certs/ca-bundle.crt",
]
# Profile path candidates for `$env.NIX_SSL_CERT_FILE`.
let profile_paths = if (nix-profile?) {[
# Fall back to cacert in Nix profile.
$"(nix-profile)/etc/ssl/certs/ca-bundle.crt",
# Old cacert in Nix profile.
$"(nix-profile)/etc/ca-bundle.crt"
]}
# All path candidates for `$env.NIX_SSL_CERT_FILE`.
let all_paths = ($system_paths | append $profile_paths)
# Find the first existing path and select it for `$env.NIX_SSL_CERT_FILE`.
# If none exists, returns `$nothing`.
let matches = ($all_paths | find -p { |path| $path | path exists })
if (not ($matches | empty?)) { $matches | first }
}
# Set `$env.NIX_PROFILES` with nix profile paths (default and user).
export env NIX_PROFILES {
let default = "/nix/var/nix/profiles/default"
if (nix-profile?) {
$"($default) (nix-profile)"
} else {
$default
}
}
# Only use `$env.MANPATH` if it is already set. In general `man` will just simply
# pick up `.nix-profile/share/man` because is it close to `.nix-profile/bin`
# which is in the `$env.PATH`. For more info, run `manpath -d`.
export env MANPATH {
if ("MANPATH" in (env).name) {
if (nix-profile?) {
$env.MANPATH | nix-path $"(nix-profile)/share/man"
} else {
$env.MANPATH
}
} # Else `$nothing`.
}
# Prepend `$env.PATH` with `.nix-profile/bin`, removing duplicates.
export env PATH {
if ("PATH" in (env).name) {
if (nix-profile?) {
$env.PATH | nix-path $"(nix-profile)/bin"
} else {
$env.PATH
}
} # Else `$nothing`.
}
}
use nix-profile [NIX_SSL_CERT_FILE, NIX_PROFILES, MANPATH, PATH]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment