Skip to content

Commit

Permalink
✨ Trying the repl
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuDR committed Nov 18, 2024
1 parent a2c8c49 commit b43d119
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
./hosts
./home-manager
./modules
./pkgs
./pre-commit-hooks.nix
];

Expand All @@ -73,6 +74,7 @@
pkgs.just
pkgs.fzf
pkgs.nodePackages.prettier
(config.packages.repl)
];
name = "dots";
DIRENV_LOG_FORMAT = "";
Expand Down
49 changes: 49 additions & 0 deletions lib/repl.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
flakePath ? null,
hostnamePath ? "/etc/hostname",
registryPath ? /etc/nix/registry.json,
}: let
inherit (builtins) getFlake head match currentSystem readFile pathExists filter fromJSON;

selfFlake =
if pathExists registryPath
then filter (it: it.from.id == "self") (fromJSON (readFile registryPath)).flakes
else [];

flakePath' =
toString
(
if flakePath != null
then flakePath
else if selfFlake != []
then (head selfFlake).to.path
else "/etc/nixos"
);

flake =
if pathExists flakePath'
then getFlake flakePath'
else {};
hostname =
if pathExists hostnamePath
then head (match "([a-zA-Z0-9\\-]+)\n" (readFile hostnamePath))
else "";

nixpkgsFromInputsPath = flake.inputs.nixpkgs.outPath or "";
nixpkgs =
flake.pkgs.${currentSystem}.nixpkgs
or (
if nixpkgsFromInputsPath != ""
then import nixpkgsFromInputsPath {}
else {}
);

nixpkgsOutput = removeAttrs (nixpkgs // nixpkgs.lib or {}) ["options" "config"];
in
{inherit flake;}
// flake
// builtins
// (flake.nixosConfigurations or {})
// flake.nixosConfigurations.${hostname} or {}
// nixpkgsOutput
// {getFlake = path: getFlake (toString path);}
10 changes: 10 additions & 0 deletions pkgs/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
systems = ["x86_64-linux"];

perSystem = {pkgs, ...}: {
packages = {
# instant repl with automatic flake loading
repl = pkgs.callPackage ./repl {};
};
};
}
25 changes: 25 additions & 0 deletions pkgs/repl/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# modified from https://github.com/gytis-ivaskevicius/flake-utils/plus
{
coreutils,
gnused,
writeShellScriptBin,
}: let
repl = ../../lib/repl.nix;
example = command: desc: ''\n\u001b[33m ${command}\u001b[0m - ${desc}'';
in
writeShellScriptBin "repl" ''
case "$1" in
"-h"|"--help"|"help")
printf "%b\n\e[4mUsage\e[0m: \
${example "repl" "Loads system flake if available."} \
${example "repl /path/to/flake.nix" "Loads specified flake."}\n"
;;
*)
if [ -z "$1" ]; then
nix repl ${repl}
else
nix repl --arg flakePath $(${coreutils}/bin/readlink -f $1 | ${gnused}/bin/sed 's|/flake.nix||') ${repl}
fi
;;
esac
''

0 comments on commit b43d119

Please sign in to comment.