-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.nix
151 lines (127 loc) · 4.14 KB
/
shell.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
{
pkgs ? import <nixpkgs> {},
fenix ? import <fenix> {},
}: let
# Helpful nix function
getLibFolder = pkg: "${pkg}/lib";
# Rust Toolchain via fenix
toolchain = fenix.packages.${pkgs.system}.fromToolchainFile {
file = ./rust-toolchain.toml;
# Don't worry, if you need sha256 of your toolchain,
# just run `nix build` and copy paste correct sha256.
sha256 = "sha256-Hn2uaQzRLidAWpfmRwSRdImifGUCAb9HeAqTYFXWeQk=";
};
# Manifest via Cargo.toml
manifest = (pkgs.lib.importTOML ./Cargo.toml).package;
in
pkgs.stdenv.mkDerivation {
name = "${manifest.name}-dev";
# Compile time dependencies
nativeBuildInputs = with pkgs; [
# GCC toolchain
gcc
gnumake
pkg-config
# LLVM toolchain
cmake
llvmPackages.llvm
llvmPackages.clang
# Hail the Nix
nixd
statix
deadnix
alejandra
# Rust
toolchain
cargo-watch
# Other compile time dependencies
openssl
# libressl
];
# Runtime dependencies which will be shipped
# with nix package
buildInputs = with pkgs; [
openssl
# libressl
];
# Set Environment Variables
RUST_BACKTRACE = "full";
# Compiler LD variables
# > Make sure packages have /lib or /include path'es
NIX_LDFLAGS = "-L${(getLibFolder pkgs.libiconv)}";
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
pkgs.gcc
pkgs.libiconv
pkgs.llvmPackages.llvm
];
# Touch only if you know what you're doing
# (bash skill issues are not welcomed here)
shellHook = ''
# Check for .env file
if [ ! -f .env ]; then
read -r -p "Please enter your telegram bot token: " TELOXIDE_TOKEN;
echo "export TELOXIDE_TOKEN=$TELOXIDE_TOKEN" > .env;
echo "export WATCH_MODE=false" >> .env;
fi
# Load everything from .env
source .env;
# Lesss goooo...
if [ $WATCH_MODE == true ]; then
echo "Watchmode in background is enabled!"
echo "Starting cargo watch session on backround..."
echo
start() {
# Start watching for changes in the background
cargo watch -x "run --bin ${manifest.name} -- env" &
echo
echo
echo ===========================================================================
echo "Press enter to continue on your terminal"
echo "\> Don't close this terminal as bot is running on watch mode and see log"
echo "\> Just open your editor from this terminal to make your editor read PATH"
echo ===========================================================================
# Store the PID of the background process to file & env
CARGO_WATCH_PID=$!
echo "$CARGO_WATCH_PID" > .daemon
# Function to clean up the background process on exit
cleanup() {
kill $CARGO_WATCH_PID || true
rm -rf ./.daemon
}
# Trap EXIT signal to run cleanup function
trap cleanup EXIT INT TERM
}
# Check if there's already session running
if [ -e ".daemon" ]; then
echo "There's a session already running..."
read -p "would you like to stop running instance? (y/n): " answer
case "$answer" in
[Yy]*)
# Read old session file
_pid=$(<.daemon)
# Kill & remove old session
kill $_pid || true
# Cleanup daemon
rm -rf ./.daemon
# Cleanup the pid variable
unset _pid
# Start the damn bot
start
;;
[Nn]*)
echo "Aight, no problem bro. I got you..."
;;
*)
echo "Invalid input, ignoring running instance and not running anything"
;;
esac
else
# Start the damn bot
start
fi
else
echo "You disabled the watch mode on background."
echo "Feel free to enable it back again at .env file!"
fi
'';
}