-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |