Skip to content

Commit

Permalink
Add init nix shell command
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrw committed Aug 9, 2023
1 parent 2000d41 commit e00939a
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions home/bin/init-nix-shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python

"""
Create a shell.nix with default settings in the current directory, and set the .envrc file to "use nix"
"""

import argparse
import logging
from pathlib import Path
import sys

logging.basicConfig(level=logging.WARNING)
LOG = logging.getLogger(__name__)


SHELL_NIX_TEMPLATE = """{ pkgs ? import <nixpkgs> { } }:
with pkgs;
mkShell {
packages = [
];
}
"""


def init_shell_nix(path: Path):
with path.open("w") as outfile:
print(SHELL_NIX_TEMPLATE, file=outfile)


def init_envrc(path: Path):
with path.open("w") as outfile:
print("use nix", file=outfile)


def init_dir(root: Path, force: bool):
LOG.info(f"initialising {root}")

shell_nix_path = root.joinpath("shell.nix")
envrc_path = root.joinpath(".envrc")

if shell_nix_path.is_file() and not force:
raise ValueError(f"file exists at {shell_nix_path}")

if envrc_path.is_file() and not force:
raise ValueError(f"file exists at {envrc_path}")

init_shell_nix(shell_nix_path)
init_envrc(envrc_path)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", action="store_true", default=False)
parser.add_argument("-r", "--root-dir", required=False, default=Path.cwd(), type=Path)
parser.add_argument("-f", "--force", action="store_true", default=False)
args = parser.parse_args()

if args.verbose:
LOG.setLevel(logging.DEBUG)

try:
init_dir(args.root_dir.resolve(), args.force)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
raise SystemExit(1)

0 comments on commit e00939a

Please sign in to comment.