GradientTape not computing gradient
Since you are trying to compute $\partial loss \over \partial x$ you need to perform all operations that lead from $x$ to $loss$ inside GradientTape's scope, so that it can monitor them.

x = tf.Variable(2.)

with tf.GradientTape(persistent=True) as g:
    loss_ = x**2-2*x+1
    loss = loss_*1

print(g.gradient(loss,x))
# Will output: tf.Tensor(2.0, shape=(), dtype=float32)