Skip to content

Commit

Permalink
Initial support for nix shell/develop
Browse files Browse the repository at this point in the history
Nix shell/develop provides similar functionality to 'magic shell' but works with the
Nix package manager (https://nixos.org/download/) and is generally used to manage
an entire operating system.
Nix does not follow the FHS standard which means that magic does not work on systems
using Nix out of the box and providing such a developer environment makes it easier
for people using Nix to use and contribute to the software provided by Modular.
On a system with a default Nix installation the shell can be entered by going to the
mojo repository path and executing 'nix-shell'.
With flake support enabled it also works with 'nix develop'.

Signed-off-by: Thomas Mader <[email protected]>
  • Loading branch information
ThomasMader committed Sep 26, 2024
1 parent 7d43872 commit ff0877d
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
77 changes: 77 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
description = "Magic development environment";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
};

outputs = { self, nixpkgs, flake-utils, flake-compat }:
let
linuxPkgs = nixpkgs.legacyPackages."x86_64-linux";
darwinPkgs = nixpkgs.legacyPackages."aarch64-darwin";
forAllSystems = function:
nixpkgs.lib.genAttrs [
"x86_64-linux"
"aarch64-darwin"
] (system: function nixpkgs.legacyPackages.${system});
version = "0.3.0";
getBinary = system: {
"x86_64-linux" = "magic-x86_64-unknown-linux-musl";
"aarch64-darwin" = "magic-aarch64-apple-darwin";
}.${system} or (throw "unsupported system: ${system}");
fetchMagic = pkgs: binary: pkgs.stdenv.mkDerivation rec {
name = "magic";
src = pkgs.fetchurl {
url = "https://dl.modular.com/public/magic/raw/versions/${version}/${binary}";
sha256 = "sha256-wNweaK1TPqsVv9D1x7/m5kr1tryWfftzKCKDNdVBkOc=";
};

dontUnpack = true;

postInstall = ''
mkdir -p $out/bin
cp $src $out/bin/magic
chmod +x $out/bin/magic
'';
};
magicEnv = pkgs: magic: (pkgs.buildFHSEnv {
name = "magic-shell";
targetPkgs = pkgs: (with pkgs; [
libz
clang
lit
llvm
# Magic provides currently ncurses 6.5 on it's own but libtinfo is
# needed and in nixpkgs this does not seem to be provided separately
# https://github.com/NixOS/nixpkgs/issues/89769
ncurses
]);
profile = ''
MODULAR_HOME=$HOME/.modular
BIN_DIR=$MODULAR_HOME/bin
MAGIC=$BIN_DIR/magic
if [ ! -e $MAGIC ]; then
mkdir -p $BIN_DIR
cp ${magic}/bin/magic $BIN_DIR/magic
echo 37e8aeee-7585-494b-83e4-59244188c2fe > "$MODULAR_HOME/webUserId"
fi
export PATH=$BIN_DIR:$PATH
# Seems to work but prints error 'complete: command not found' when
# the shell is entered.
# Maybe related to https://github.com/NixOS/nix/issues/6091#issuecomment-1038247010
eval "$(magic completion --shell bash)"
'';
runScript = ''
magic shell
'';
}).env;
in
{
devShells = forAllSystems (pkgs: {
default = magicEnv pkgs (fetchMagic pkgs (getBinary pkgs.system));
});
};
}

13 changes: 13 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# To support non-flake-enabled nix instances.
(import
(
let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in
fetchTarball {
url = lock.nodes.flake-compat.locked.url or
"https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{ src = ./.; }
).shellNix

0 comments on commit ff0877d

Please sign in to comment.